亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

directory search
archive archive/tar archive/zip bufio bufio(緩存) builtin builtin(內(nèi)置包) bytes bytes(包字節(jié)) compress compress/bzip2(壓縮/bzip2) compress/flate(壓縮/flate) compress/gzip(壓縮/gzip) compress/lzw(壓縮/lzw) compress/zlib(壓縮/zlib) container container/heap(容器數(shù)據(jù)結(jié)構(gòu)heap) container/list(容器數(shù)據(jù)結(jié)構(gòu)list) container/ring(容器數(shù)據(jù)結(jié)構(gòu)ring) context context(上下文) crypto crypto(加密) crypto/aes(加密/aes) crypto/cipher(加密/cipher) crypto/des(加密/des) crypto/dsa(加密/dsa) crypto/ecdsa(加密/ecdsa) crypto/elliptic(加密/elliptic) crypto/hmac(加密/hmac) crypto/md5(加密/md5) crypto/rand(加密/rand) crypto/rc4(加密/rc4) crypto/rsa(加密/rsa) crypto/sha1(加密/sha1) crypto/sha256(加密/sha256) crypto/sha512(加密/sha512) crypto/subtle(加密/subtle) crypto/tls(加密/tls) crypto/x509(加密/x509) crypto/x509/pkix(加密/x509/pkix) database database/sql(數(shù)據(jù)庫(kù)/sql) database/sql/driver(數(shù)據(jù)庫(kù)/sql/driver) debug debug/dwarf(調(diào)試/dwarf) debug/elf(調(diào)試/elf) debug/gosym(調(diào)試/gosym) debug/macho(調(diào)試/macho) debug/pe(調(diào)試/pe) debug/plan9obj(調(diào)試/plan9obj) encoding encoding(編碼) encoding/ascii85(編碼/ascii85) encoding/asn1(編碼/asn1) encoding/base32(編碼/base32) encoding/base64(編碼/base64) encoding/binary(編碼/binary) encoding/csv(編碼/csv) encoding/gob(編碼/gob) encoding/hex(編碼/hex) encoding/json(編碼/json) encoding/pem(編碼/pem) encoding/xml(編碼/xml) errors errors(錯(cuò)誤) expvar expvar flag flag(命令行參數(shù)解析flag包) fmt fmt go go/ast(抽象語(yǔ)法樹) go/build go/constant(常量) go/doc(文檔) go/format(格式) go/importer go/parser go/printer go/scanner(掃描儀) go/token(令牌) go/types(類型) hash hash(散列) hash/adler32 hash/crc32 hash/crc64 hash/fnv html html html/template(模板) image image(圖像) image/color(顏色) image/color/palette(調(diào)色板) image/draw(繪圖) image/gif image/jpeg image/png index index/suffixarray io io io/ioutil log log log/syslog(日志系統(tǒng)) math math math/big math/big math/bits math/bits math/cmplx math/cmplx math/rand math/rand mime mime mime/multipart(多部分) mime/quotedprintable net net net/http net/http net/http/cgi net/http/cookiejar net/http/fcgi net/http/httptest net/http/httptrace net/http/httputil net/http/internal net/http/pprof net/mail net/mail net/rpc net/rpc net/rpc/jsonrpc net/smtp net/smtp net/textproto net/textproto net/url net/url os os os/exec os/signal os/user path path path/filepath(文件路徑) plugin plugin(插件) reflect reflect(反射) regexp regexp(正則表達(dá)式) regexp/syntax runtime runtime(運(yùn)行時(shí)) runtime/debug(調(diào)試) runtime/internal/sys runtime/pprof runtime/race(競(jìng)爭(zhēng)) runtime/trace(執(zhí)行追蹤器) sort sort(排序算法) strconv strconv(轉(zhuǎn)換) strings strings(字符串) sync sync(同步) sync/atomic(原子操作) syscall syscall(系統(tǒng)調(diào)用) testing testing(測(cè)試) testing/iotest testing/quick text text/scanner(掃描文本) text/tabwriter text/template(定義模板) text/template/parse time time(時(shí)間戳) unicode unicode unicode/utf16 unicode/utf8 unsafe unsafe
characters

  • import "strconv"

  • 概觀

  • 索引

  • 示例

概觀

包strconv實(shí)現(xiàn)了對(duì)基本數(shù)據(jù)類型的字符串表示的轉(zhuǎn)換。

Numeric Conversions

最常見的數(shù)值轉(zhuǎn)換是 Atoi(string to int)和 Itoa(int to string)。

i, err := strconv.Atoi("-42")s := strconv.Itoa(-42)

這些假設(shè)小數(shù)和 Go int 類型。

ParseBool,ParseFloat,ParseInt 和 ParseUint 將字符串轉(zhuǎn)換為值:

b, err := strconv.ParseBool("true")f, err := strconv.ParseFloat("3.1415", 64)i, err := strconv.ParseInt("-42", 10, 64)u, err := strconv.ParseUint("42", 10, 64)

解析函數(shù)返回最寬的類型(float64,int64和uint64),但如果 size 參數(shù)指定較窄的寬度,則結(jié)果可以轉(zhuǎn)換為較窄的類型而不會(huì)丟失數(shù)據(jù):

s := "2147483647" // biggest int32i64, err := strconv.ParseInt(s, 10, 32)...i := int32(i64)

FormatBool,F(xiàn)ormatFloat,F(xiàn)ormatInt 和 FormatUint將值轉(zhuǎn)換為字符串:

s := strconv.FormatBool(true)s := strconv.FormatFloat(3.1415, 'E', -1, 64)s := strconv.FormatInt(-42, 16)s := strconv.FormatUint(42, 16)

AppendBool,AppendFloat,AppendInt 和 AppendUint 是相似的,但將格式化后的值附加到目標(biāo)切片。

字符串轉(zhuǎn)換

Quote 和 QuoteToASCII 將字符串轉(zhuǎn)換為帶引號(hào)的字符串文字。后者保證結(jié)果是一個(gè) ASCII 字符串,用 \ u 轉(zhuǎn)義任何非 ASCII 的 Unicode:

q := Quote("Hello, 世界")q := QuoteToASCII("Hello, 世界")

QuoteRune 和 QuoteRuneToASCII 類似,但接受符文并返回引用的去符文字面值。

Unquote 和 UnquoteChar 不引用 Go 字符串和符文字面值。

索引

  • 常量

  • 變量

  • func AppendBool(dst []byte, b bool) []byte

  • func AppendFloat(dst []byte, f float64, fmt byte, prec, bitSize int) []byte

  • func AppendInt(dst []byte, i int64, base int) []byte

  • func AppendQuote(dst []byte, s string) []byte

  • func AppendQuoteRune(dst []byte, r rune) []byte

  • func AppendQuoteRuneToASCII(dst []byte, r rune) []byte

  • func AppendQuoteRuneToGraphic(dst []byte, r rune) []byte

  • func AppendQuoteToASCII(dst []byte, s string) []byte

  • func AppendQuoteToGraphic(dst []byte, s string) []byte

  • func AppendUint(dst []byte, i uint64, base int) []byte

  • func Atoi(s string) (int, error)

  • func CanBackquote(s string) bool

  • func FormatBool(b bool) string

  • func FormatFloat(f float64, fmt byte, prec, bitSize int) string

  • func FormatInt(i int64, base int) string

  • func FormatUint(i uint64, base int) string

  • func IsGraphic(r rune) bool

  • func IsPrint(r rune) bool

  • func Itoa(i int) string

  • func ParseBool(str string) (bool, error)

  • func ParseFloat(s string, bitSize int) (float64, error)

  • func ParseInt(s string, base int, bitSize int) (i int64, err error)

  • func ParseUint(s string, base int, bitSize int) (uint64, error)

  • func Quote(s string) string

  • func QuoteRune(r rune) string

  • func QuoteRuneToASCII(r rune) string

  • func QuoteRuneToGraphic(r rune) string

  • func QuoteToASCII(s string) string

  • func QuoteToGraphic(s string) string

  • func Unquote(s string) (string, error)

  • func UnquoteChar(s string, quote byte) (value rune, multibyte bool, tail string, err error)

  • type NumError

  • func (e *NumError) Error() string

示例

AppendBool AppendFloat AppendInt AppendQuote AppendQuoteRune AppendQuoteRuneToASCII AppendQuoteToASCII AppendUint Atoi CanBackquote FormatBool FormatFloat FormatInt FormatUint IsPrint Itoa NumError ParseBool ParseFloat ParseInt ParseUint Quote QuoteRune QuoteRuneToASCII QuoteToASCII Unquote UnquoteChar

打包文件

atob.go atof.go atoi.go decimal.go doc.go extfloat.go ftoa.go isprint.go itoa.go quote.go

常量

IntSize 是 int 或 uint 值的大小(以位為單位)。

const IntSize = intSize

變量

ErrRange 表示目標(biāo)類型的值超出范圍。

var ErrRange = errors.New("value out of range")

ErrSyntax 表明一個(gè)值沒有針對(duì)目標(biāo)類型的正確語(yǔ)法。

var ErrSyntax = errors.New("invalid syntax")

func AppendBool

func AppendBool(dst []byte, b bool) []byte

AppendBool 根據(jù) b 的值將“true”或“false”附加到 dst 并返回?cái)U(kuò)展緩沖區(qū)。

示例

package mainimport ("fmt""strconv")func main() {
	b := []byte("bool:")
	b = strconv.AppendBool(b, true)
	fmt.Println(string(b))}

func AppendFloat

func AppendFloat(dst []byte, f float64, fmt byte, prec, bitSize int) []byte

AppendFloat 將由 FormatFloat 生成的浮點(diǎn)數(shù) f 的字符串形式附加到 dst 并返回?cái)U(kuò)展緩沖區(qū)。

示例

package mainimport ("fmt""strconv")func main() {
	b32 := []byte("float32:")
	b32 = strconv.AppendFloat(b32, 3.1415926535, 'E', -1, 32)
	fmt.Println(string(b32))

	b64 := []byte("float64:")
	b64 = strconv.AppendFloat(b64, 3.1415926535, 'E', -1, 64)
	fmt.Println(string(b64))}

func AppendInt

func AppendInt(dst []byte, i int64, base int) []byte

AppendInt 將由 FormatInt 生成的整數(shù)i的字符串形式附加到 dst 并返回?cái)U(kuò)展緩沖區(qū)。

示例

package mainimport ("fmt""strconv")func main() {
	b10 := []byte("int (base 10):")
	b10 = strconv.AppendInt(b10, -42, 10)
	fmt.Println(string(b10))

	b16 := []byte("int (base 16):")
	b16 = strconv.AppendInt(b16, -42, 16)
	fmt.Println(string(b16))}

func AppendQuote

func AppendQuote(dst []byte, s string) []byte

AppendQuote 將由 Quote 生成的代表 s 的雙引號(hào) Go 字符串文字附加到 dst 并返回?cái)U(kuò)展緩沖區(qū)。

示例

package mainimport ("fmt""strconv")func main() {
	b := []byte("quote:")
	b = strconv.AppendQuote(b, `"Fran & Freddie's Diner"`)
	fmt.Println(string(b))}

func AppendQuoteRune

func AppendQuoteRune(dst []byte, r rune) []byte

AppendQuoteRune 將由 QuoteRune 生成的表示符文的單引號(hào) Go 字符文字附加到 dst 并返回?cái)U(kuò)展緩沖區(qū)。

示例

package mainimport ("fmt""strconv")func main() {
	b := []byte("rune:")
	b = strconv.AppendQuoteRune(b, '?')
	fmt.Println(string(b))}

func AppendQuoteRuneToASCII

func AppendQuoteRuneToASCII(dst []byte, r rune) []byte

AppendQuoteRuneToASCII 將由 QuoteRuneToASCII 生成的代表該符文的單引號(hào) Go 字符文字附加到 dst 并返回?cái)U(kuò)展緩沖區(qū)。

示例

package mainimport ("fmt""strconv")func main() {
	b := []byte("rune (ascii):")
	b = strconv.AppendQuoteRuneToASCII(b, '?')
	fmt.Println(string(b))}

func AppendQuoteRuneToGraphic

func AppendQuoteRuneToGraphic(dst []byte, r rune) []byte

AppendQuoteRuneToGraphic 將由 QuoteRuneToGraphic 生成的表示符文的單引號(hào) Go 字符文字附加到 dst 并返回?cái)U(kuò)展緩沖區(qū)。

func AppendQuoteToASCII

func AppendQuoteToASCII(dst []byte, s string) []byte

AppendQuoteToASCII 將由 QuoteToASCII 生成的代表 s 的雙引號(hào) Go 字符串文字附加到 dst 并返回?cái)U(kuò)展緩沖區(qū)。

示例

package mainimport ("fmt""strconv")func main() {
	b := []byte("quote (ascii):")
	b = strconv.AppendQuoteToASCII(b, `"Fran & Freddie's Diner"`)
	fmt.Println(string(b))}

func AppendQuoteToGraphic

func AppendQuoteToGraphic(dst []byte, s string) []byte

AppendQuoteToGraphic 將由 QuoteToGraphic 生成的代表 s 的雙引號(hào) Go 字符串文字附加到 dst 并返回?cái)U(kuò)展緩沖區(qū)。

func AppendUint

func AppendUint(dst []byte, i uint64, base int) []byte

AppendUint 將由 FormatUint 生成的無(wú)符號(hào)整數(shù) i 的字符串形式附加到 dst 并返回?cái)U(kuò)展緩沖區(qū)。

示例

package mainimport ("fmt""strconv")func main() {
	b10 := []byte("uint (base 10):")
	b10 = strconv.AppendUint(b10, 42, 10)
	fmt.Println(string(b10))

	b16 := []byte("uint (base 16):")
	b16 = strconv.AppendUint(b16, 42, 16)
	fmt.Println(string(b16))}

func Atoi

func Atoi(s string) (int, error)

Atoi 返回 ParseInt(s, 10, 0)  轉(zhuǎn)換為 int 類型的結(jié)果。

示例

package mainimport ("fmt""strconv")func main() {
	v := "10"if s, err := strconv.Atoi(v); err == nil {
		fmt.Printf("%T, %v", s, s)}}

func CanBackquote

func CanBackquote(s string) bool

CanBackquote 報(bào)告字符串 s 是否可以不改變?yōu)閱涡蟹匆?hào)字符串,而不包含 tab 以外的控制字符。

示例

package mainimport ("fmt""strconv")func main() {
	fmt.Println(strconv.CanBackquote("Fran & Freddie's Diner ?"))
	fmt.Println(strconv.CanBackquote("`can't backquote this`"))}

func FormatBool

func FormatBool(b bool) string

FormatBool 根據(jù) b 的值返回“true”或“false”

示例

package mainimport ("fmt""strconv")func main() {
	v := true
	s := strconv.FormatBool(v)
	fmt.Printf("%T, %v\n", s, s)}

func FormatFloat

func FormatFloat(f float64, fmt byte, prec, bitSize int) string

FormatFloat 根據(jù)格式 fmt 和 precision prec 將浮點(diǎn)數(shù)f轉(zhuǎn)換為字符串。它將結(jié)果進(jìn)行四舍五入,假設(shè)原始數(shù)據(jù)是從 bitSize 位的浮點(diǎn)值獲得的(float32為32,float64為64)。

格式 fmt 是 'b'(-ddddp±ddd,二進(jìn)制指數(shù)),'e'(-d.dddde±dd,十進(jìn)制指數(shù)),'E'(-d.ddddE±dd,十進(jìn)制指數(shù)),'f'(-ddd.dddd,無(wú)指數(shù)),'g'('e'表示大指數(shù),'f'表示否則)或 'G'('E'表示大指數(shù),否則'f')。

precision prec 控制由 'e','E','f','g' 和 'G' 格式打印的位數(shù)(不包括指數(shù))。對(duì)于 'e','E' 和 'f',它是小數(shù)點(diǎn)后的位數(shù)。對(duì)于 'g' 和 'G' 這是總位數(shù)。特殊精度-1使用必需的最小位數(shù),以便 ParseFloat 完全返回 f 。

示例

package mainimport ("fmt""strconv")func main() {
	v := 3.1415926535

	s32 := strconv.FormatFloat(v, 'E', -1, 32)
	fmt.Printf("%T, %v\n", s32, s32)

	s64 := strconv.FormatFloat(v, 'E', -1, 64)
	fmt.Printf("%T, %v\n", s64, s64)}

func FormatInt

func FormatInt(i int64, base int) string

FormatInt 返回給定基數(shù)中的i的字符串表示,對(duì)于2 <= base <= 36.結(jié)果對(duì)于數(shù)字值> = 10使用小寫字母 'a' 到 'z' 。

示例

package mainimport ("fmt""strconv")func main() {
	v := int64(-42)

	s10 := strconv.FormatInt(v, 10)
	fmt.Printf("%T, %v\n", s10, s10)

	s16 := strconv.FormatInt(v, 16)
	fmt.Printf("%T, %v\n", s16, s16)}

func FormatUint

func FormatUint(i uint64, base int) string

FormatUint 返回給定基數(shù)中的 i 的字符串表示,對(duì)于2 <= base <= 36.結(jié)果對(duì)于數(shù)字值> = 10使用小寫字母 'a' 到 'z' 。

示例

package mainimport ("fmt""strconv")func main() {
	v := uint64(42)

	s10 := strconv.FormatUint(v, 10)
	fmt.Printf("%T, %v\n", s10, s10)

	s16 := strconv.FormatUint(v, 16)
	fmt.Printf("%T, %v\n", s16, s16)}

func IsGraphic

func IsGraphic(r rune) bool

IsGraphic 報(bào)告符文是否被 Unicode 定義為 Graphic。這些字符包括類別 L,M,N,P,S 和 Z 中的字母,標(biāo)記,數(shù)字,標(biāo)點(diǎn),符號(hào)和空格。

func IsPrint

func IsPrint(r rune) bool

IsPrint 報(bào)告該符文是否被 Go 定義為可打印,其定義與 unicode.IsPrint 相同:字母,數(shù)字,標(biāo)點(diǎn),符號(hào)和 ASCII 空格。

示例

package mainimport ("fmt""strconv")func main() {
	c := strconv.IsPrint('\u263a')
	fmt.Println(c)

	bel := strconv.IsPrint('\007')
	fmt.Println(bel)}

func Itoa

func Itoa(i int) string

Itoa 是 FormatInt(int64(i), 10) 的縮寫。

示例

package mainimport ("fmt""strconv")func main() {
	i := 10
	s := strconv.Itoa(i)
	fmt.Printf("%T, %v\n", s, s)}

func ParseBool

func ParseBool(str string) (bool, error)

ParseBool 返回字符串表示的布爾值。它接受1,t,T,TRUE,true,True,0,f,F(xiàn),F(xiàn)ALSE,false,F(xiàn)alse。任何其他值都會(huì)返回錯(cuò)誤。

示例

package mainimport ("fmt""strconv")func main() {
	v := "true"if s, err := strconv.ParseBool(v); err == nil {
		fmt.Printf("%T, %v\n", s, s)}}

func ParseFloat

func ParseFloat(s string, bitSize int) (float64, error)

ParseFloat 將字符串 s 轉(zhuǎn)換為浮點(diǎn)數(shù),精度由 bitSize:32指定,float32為64; float64為64。當(dāng) bitSize = 32時(shí),結(jié)果仍然具有 float64 類型,但可以在不更改其值的情況下將其轉(zhuǎn)換為 float32。

如果s格式良好且接近有效的浮點(diǎn)數(shù),則 ParseFloat 返回使用 IEEE754 無(wú)偏舍入舍入的最近浮點(diǎn)數(shù)。

ParseFloat 返回的錯(cuò)誤具有具體類型 * NumError 并包含 err.Num = s。

如果 s 在語(yǔ)法上不是格式良好的,ParseFloat 返回 err.Err = ErrSyntax。

如果 s 在語(yǔ)法上格式良好,但距離給定大小的最大浮點(diǎn)數(shù)大于1/2 ULP,則 ParseFloat 返回 f =±Inf,err.Err = ErrRange。

示例

package mainimport ("fmt""strconv")func main() {
	v := "3.1415926535"if s, err := strconv.ParseFloat(v, 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)}if s, err := strconv.ParseFloat(v, 64); err == nil {
		fmt.Printf("%T, %v\n", s, s)}}

func ParseInt

func ParseInt(s string, base int, bitSize int) (i int64, err error)

ParseInt 解釋給定基礎(chǔ)(2到36)中的字符串 s 并返回相應(yīng)的值 i。如果 base == 0,則基數(shù)由字符串的前綴隱含:base 16代表“0x”,base 8代表“0”,否則以10為底數(shù)。

bitSize 參數(shù)指定結(jié)果必須適合的整數(shù)類型。位大小 0,8,16,32 和 64 對(duì)應(yīng)于 int,int8,int16,int32 和 int64。

ParseInt 返回的錯(cuò)誤具有具體類型 * NumError 并包含err.Num = s。如果s為空或包含無(wú)效數(shù)字,則 err.Err = ErrSyntax,返回值為0; 如果與s對(duì)應(yīng)的值不能用給定大小的有符號(hào)整數(shù)表示,則 err.Err = ErrRange,返回的值是相應(yīng) bitSize 和符號(hào)的最大幅度整數(shù)。

示例

package mainimport ("fmt""strconv")func main() {
	v32 := "-354634382"if s, err := strconv.ParseInt(v32, 10, 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)}if s, err := strconv.ParseInt(v32, 16, 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)}

	v64 := "-3546343826724305832"if s, err := strconv.ParseInt(v64, 10, 64); err == nil {
		fmt.Printf("%T, %v\n", s, s)}if s, err := strconv.ParseInt(v64, 16, 64); err == nil {
		fmt.Printf("%T, %v\n", s, s)}}

func ParseUint

func ParseUint(s string, base int, bitSize int) (uint64, error)

ParseUint 就像 ParseInt,但是對(duì)于無(wú)符號(hào)數(shù)字。

示例

package mainimport ("fmt""strconv")func main() {
	v := "42"if s, err := strconv.ParseUint(v, 10, 32); err == nil {
		fmt.Printf("%T, %v\n", s, s)}if s, err := strconv.ParseUint(v, 10, 64); err == nil {
		fmt.Printf("%T, %v\n", s, s)}}

func Quote

func Quote(s string) string

Quote 返回一個(gè)雙引號(hào)的 Go 字符串字面表示s。返回的字符串使用 Go 轉(zhuǎn)義序列 (\t, \n, \xFF, \u0100) 作為 IsPrint 定義的控制字符和非可打印字符。

示例

package mainimport ("fmt""strconv")func main() {
	s := strconv.Quote(`"Fran & Freddie's Diner	?"`)
	fmt.Println(s)}

func QuoteRune

func QuoteRune(r rune) string

QuoteRune 返回一個(gè)表示符文的單引號(hào) Go 字符。返回的字符串使用 Go 轉(zhuǎn)義序列(\t, \n, \xFF, \u0100) 作為 IsPrint 定義的控制字符和非可打印字符。

示例

package mainimport ("fmt""strconv")func main() {
	s := strconv.QuoteRune('?')
	fmt.Println(s)}

func QuoteRuneToASCII

func QuoteRuneToASCII(r rune) string

QuoteRuneToASCII 返回表示符文的單引號(hào) Go 字符。對(duì)于非 ASCII 字符和 IsPrint 定義的非可打印字符,返回的字符串使用 Go 轉(zhuǎn)義序列  (\t, \n, \xFF, \u0100)。

示例

package mainimport ("fmt""strconv")func main() {
	s := strconv.QuoteRuneToASCII('?')
	fmt.Println(s)}

func QuoteRuneToGraphic

func QuoteRuneToGraphic(r rune) string

QuoteRuneToGraphic 返回代表符文的單引號(hào) Go 字符。對(duì)于非 ASCII 字符和 IsGraphic 定義的非可打印字符,返回的字符串使用Go轉(zhuǎn)義序列 (\t, \n, \xFF, \u0100)。

func QuoteToASCII

func QuoteToASCII(s string) string

QuoteToASCII 返回一個(gè)代表 s 的雙引號(hào) Go 字符串。對(duì)于非 ASCII 字符和 IsPrint 定義的非可打印字符,返回的字符串使用 Go 轉(zhuǎn)義序列 (\t, \n, \xFF, \u0100) 。

示例

package mainimport ("fmt""strconv")func main() {
	s := strconv.QuoteToASCII(`"Fran & Freddie's Diner	?"`)
	fmt.Println(s)}

func QuoteToGraphic

func QuoteToGraphic(s string) string

QuoteToGraphic 返回一個(gè)代表 s 的雙引號(hào) Go 字符串。對(duì)于非 ASCII 字符和 IsGraphic 定義的非可打印字符,返回的字符串使用 Go 轉(zhuǎn)義序列 (\t, \n, \xFF, \u0100)。

func Unquote

func Unquote(s string) (string, error)

Unquote 將 s 解釋為單引號(hào),雙引號(hào)或反引號(hào)的 Go 字符串文字,返回引用的字符串值。(如果 s 是單引號(hào),它將是一個(gè) Go 字符字面量; Unquote 會(huì)返回相應(yīng)的一個(gè)字符字符串。)

示例

package mainimport ("fmt""strconv")func main() {
	test := func(s string) {
		t, err := strconv.Unquote(s)if err != nil {
			fmt.Printf("Unquote(%#v): %v\n", s, err)} else {
			fmt.Printf("Unquote(%#v) = %v\n", s, t)}}

	s := `\"Fran & Freddie's Diner\t\u263a\"\"`// If the string doesn't have quotes, it can't be unquoted.test(s) // invalid syntaxtest("`" + s + "`")test(`"` + s + `"`)test(`'\u263a'`)}

func UnquoteChar

func UnquoteChar(s string, quote byte) (value rune, multibyte bool, tail string, err error)

UnquoteChar 解碼轉(zhuǎn)義字符串中的第一個(gè)字符或字節(jié)或由字符串 s 表示的字符字面值。它返回四個(gè)值:

1) value, the decoded Unicode code point or byte value;2) multibyte, a boolean indicating whether the decoded character requires a multibyte UTF-8 representation;3) tail, the remainder of the string after the character; and4) an error that will be nil if the character is syntactically valid.

第二個(gè)參數(shù) quote 指定了被解析的文字類型,因此允許使用哪個(gè)轉(zhuǎn)義引號(hào)字符。如果設(shè)置為單引號(hào),則允許序列 \'并且不允許未轉(zhuǎn)義'。如果設(shè)置為雙引號(hào),則允許 \“并禁止未轉(zhuǎn)義”。如果設(shè)置為零,它不允許任何轉(zhuǎn)義,并允許兩個(gè)引號(hào)字符顯示為未轉(zhuǎn)義。

示例

package mainimport ("fmt""log""strconv")func main() {
	v, mb, t, err := strconv.UnquoteChar(`\"Fran & Freddie's Diner\"`, '"')if err != nil {
		log.Fatal(err)}

	fmt.Println("value:", string(v))
	fmt.Println("multibyte:", mb)
	fmt.Println("tail:", t)}

type NumError

NumError 記錄轉(zhuǎn)換失敗。

type NumError struct {
        Func string // the failing function (ParseBool, ParseInt, ParseUint, ParseFloat)
        Num  string // the input
        Err  error  // the reason the conversion failed (ErrRange, ErrSyntax)}

示例

package mainimport ("fmt""strconv")func main() {
	str := "Not a number"if _, err := strconv.ParseFloat(str, 64); err != nil {
		e := err.(*strconv.NumError)
		fmt.Println("Func:", e.Func)
		fmt.Println("Num:", e.Num)
		fmt.Println("Err:", e.Err)
		fmt.Println(err)}}

func (*NumError) Error

func (e *NumError) Error() string
Previous article: Next article: