?? ??: 1. strconv ???? Atoi() ??? ???? ??? ?? ??? int ???? ?????. ??? "strconv.Atoi(string)"???. 2. ParseInt() ??? ?????. strconv ???? ???(?? ? ?? ?? ??)? ???? ?? ?? ??? ? ??? ??? "strconv.ParseInt(string,10,64)"???.
? ????? ?? ??: windows10 ???, GO 1.18, thinkpad t480 ???.
golang?? ???? ??? int ??? ???? ??
string? int?:
int, err := strconv.Atoi(string)
string? int64?:
int64, err := strconv.ParseInt(string, 10, 64)
int? string??:
string := strconv.Itoa(int)
int64? string??:
string := strconv.FormatInt(int64,10)
?? Go ??? strconv(?? ??)
Go ??? strconv
???? ?? ??? ??? ??? ?? ?? ?? ??? ?????. strconv
包實現(xiàn)了基本數(shù)據(jù)類型和其字符串表示的相互轉(zhuǎn)換。
strconv包
strconv包實現(xiàn)了基本數(shù)據(jù)類型與其字符串表示的轉(zhuǎn)換,主要有以下常用函數(shù): Atoi()
、Itia()
、parse系列、format系列、append系列。
更多函數(shù)請查看官方文檔。
string與int類型轉(zhuǎn)換
這一組函數(shù)是我們平時編程中用的最多的。
Atoi()
Atoi()
函數(shù)用于將字符串類型的整數(shù)轉(zhuǎn)換為int類型,函數(shù)簽名如下。
func Atoi(s string) (i int, err error)
如果傳入的字符串參數(shù)無法轉(zhuǎn)換為int類型,就會返回錯誤。
s1 := "100" i1, err := strconv.Atoi(s1) if err != nil { fmt.Println("can't convert to int") } else { fmt.Printf("type:%T value:%#v\n", i1, i1) //type:int value:100 }
Itoa()
Itoa()
函數(shù)用于將int類型數(shù)據(jù)轉(zhuǎn)換為對應(yīng)的字符串表示,具體的函數(shù)簽名如下。
func Itoa(i int) string
示例代碼如下:
i2 := 200s2 := strconv.Itoa(i2)fmt.Printf("type:%T value:%#v\n", s2, s2) //type:string value:"200"
Parse系列函數(shù)
Parse類函數(shù)用于轉(zhuǎn)換字符串為給定類型的值:ParseBool()、ParseFloat()、ParseInt()、ParseUint()。
ParseBool()
func ParseBool(str string) (value bool, err error)
返回字符串表示的bool值。它接受1、0、t、f、T、F、true、false、True、False、TRUE、FALSE;否則返回錯誤。
ParseInt()
func ParseInt(s string, base int, bitSize int) (i int64, err error)
返回字符串表示的整數(shù)值,接受正負(fù)號。
base指定進(jìn)制(2到36),如果base為0,則會從字符串前置判斷,”0x”是16進(jìn)制,”0”是8進(jìn)制,否則是10進(jìn)制;
bitSize指定結(jié)果必須能無溢出賦值的整數(shù)類型,0、8、16、32、64 分別代表 int、int8、int16、int32、int64;
返回的err是*NumErr類型的,如果語法有誤,err.Error = ErrSyntax;如果結(jié)果超出類型范圍err.Error = ErrRange。
ParseUnit()
func ParseUint(s string, base int, bitSize int) (n uint64, err error)
ParseUint
類似ParseInt
strconv ???
strconv ???? ?? ??? ??? ?? ??? ??? ??? ?????.Atoi()
, Itia()< /code >, ?? ?? ??, ?? ?? ??, ?? ??. <p></p>? ?? ??? ??? ?? ??? ?????. <p></p><p></p>??? ? ??? ??<p><strong></strong></p>? ?? ??? ?? ??????? ?? ????? ?????. <p></p><p>Atoi()<strong><span style="font-size: 18px;"></span><code>Atoi()
??? ??? ?? ??? int ???? ???? ? ?????. func ParseFloat(s string, bitSize int) (f float64, err error)???? ??? ????? int ???? ??? ? ?? ?? ??? ?????.
b, err := strconv.ParseBool("true") f, err := strconv.ParseFloat("3.1415", 64) i, err := strconv.ParseInt("-2", 10, 64) u, err := strconv.ParseUint("2", 10, 64)Itoa()
Itoa()
??? int ?? ???? ?? ??? ???? ???? ? ?????.
func FormatBool(b bool) string?? ??? ??? ????.
func FormatInt(i int64, base int) string
Parse ?? ??
Parse ??? ??? ???? ?? ??? ??? ???? ? ?????: ParseBool(), ParseFloat(), ParseInt() , ParseUint().ParseBool()
func FormatUint(i uint64, base int) string???? ???? ?? ?? ?????. 1, 0, t, f, T, F, true, false, True, False, TRUE, FALSE? ?????. ??? ??? ??? ?????.
ParseInt()
func FormatFloat(f float64, fmt byte, prec, bitSize int) string?? ? ?? ??? ???? ???? ???? ?? ?? ?????.
base? ??(2~36)? ?????. base? 0?? ???? ???? ?????. "0x"? 16????, "0"? 8?????.
bitSize? ?? ?????. ??? ???? ?? ??? ? ?? ?? ????? ???. 0, 8, 16, 32 ? 64? ?? int, int8, int16, int32 ? int64? ?????. ??????? ??? *NumErr ?????. ??? ???? ????. err .Error = ErrSyntax; ??? ?? ??? ???? ?? err.Error = ErrRange. ??????ParseUnit()????s1 := strconv.FormatBool(true) s2 := strconv.FormatFloat(3.1415, 'E', -1, 64) s3 := strconv.FormatInt(-2, 16) s4 := strconv.FormatUint(2, 16)??
ParseUint
? ParseInt
? ????? ?? ?? ??? ???? ??? ???? ????. ??????ParseFloat()????func IsPrint(r rune) bool?? ?? ??? ??? ???? ???? ?? ???? ?? ?? ?????. ????s? ???? ?? ??? s? ???? ?? ?? ??? ?? ??? ??? ?????(IEEE754 ?? ??? ??). ????bitSize? ???? ?? ??? ?????. 32? float32(?? ?? ??? ?? ???? ?? float32? ??? ? ??), 64? float64???. ?????? ? err? *NumErr ???? ??? ???????. err .Error =ErrSyntax; ??? ??? ???? ?? ?? ? f? ±Inf, err.Error= ErrRange???. ???????? ??????
func CanBackquote(s string) bool??? ???? ? ?? ?? ?? ????. ? ?? ?? ?? ??? ??? ? ?? ?? ?? ?? ??? ?? ?? ??????. ?????????? ?? ???????????? ?? ??? ??? ??? ???? ??? ??? ???? ????? ??? ?????. ??????FormatBool()????rrreee??b ?? ?? "true" ?? "false"? ?????. ??????FormatInt()????rrreee?? i? ?? ???? ?? ??? ??? ?????. ??? 2?? 36 ???? ?? 10?? ? ??? ???? ?? ??? ??? 'a' ~ 'z'? ?????. ??????FormatUint()????rrreee??? FormatInt? ?? ?? ?? ?????. ??????FormatFloat()????rrreee?? ??? ?? ??? ??? ???? ???? ?? ?????. ????bitSize? f? ?? ??(32: float32, 64: float64)? ???? ?? ?? ??????. ????fmt ?? ??: 'f'(-ddd.dddd), 'b'(-ddddp±ddd, ??? ???), 'e'(-d.dddde±dd, ??? ??), 'E'(- d.ddddE±dd, ??? ??), 'g'(??? ?? ? ?? 'e' ??? ????, ??? ??? 'f' ??? ??), 'G'(??? ?? ? ?? 'E' ??? ????, ??? ??? 'f' ??). ????prec? ???? ?????(?? ?? ??). 'f', 'e', ????'E'? ?? ??? ?? ???? ???? 'g', 'G'? ?? ??? ?? ???? ?????. ? ??????. prec? -1?? f? ???? ? ??? ?? ?? ??? ????? ?????. ???????? ?????
s1 := strconv.FormatBool(true) s2 := strconv.FormatFloat(3.1415, 'E', -1, 64) s3 := strconv.FormatInt(-2, 16) s4 := strconv.FormatUint(2, 16)
其他
isPrint()
func IsPrint(r rune) bool
返回一個字符是否是可打印的,和unicode.IsPrint
一樣,r必須是:字母(廣義)、數(shù)字、標(biāo)點、符號、ASCII空格。
CanBackquote()
func CanBackquote(s string) bool
返回字符串s是否可以不被修改的表示為一個單行的、沒有空格和tab之外控制字符的反引號字符串。
其他
除上文列出的函數(shù)外,strconv
包中還有Append系列、Quote系列等函數(shù)。具體用法可查看官方文檔。
推薦學(xué)習(xí):Golang教程
? ??? Go ???? ???? int ???? ???? ??? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

??? ??











Go Language?? ??? ???? ???? ?? Redisstream? ???? ??? Go Language? Redis? ???? ???? ...

???? ??? ?? ?? ???? ???? ??? ????????? Go Language ??? ?? Goland? ??? ? ?? ???? ??? ?? ?? ??? ?? ???? ...

Go Crawler Colly? ??? ??? ??? Colly Crawler ?????? GO ??? ???? ??? ?????. ? ...

GO? ?? ?????? ????? ? ??? ?? ?? ?????? ?? ????? GO? ????? ? ? ???? ?? ? ?? ???? ??? ????.

GO? ???? Oracle ??????? ??? ? Oracle ?????? ???????? GO?? ??? ? Oracle ??????? ???? ?? ???? ?? ????? ...

GO ?????? ?? ?? : MySQL ? Redis? ?? ?????? ? ??? ?? ???? ???? ???? ??? ??? ? ?? ? ??? ...

???? ?????? ??? : ???? ??? ?????? ?? GO ???? ?? "?? ??"? "???"? ??? Go? ????? ...

Go Pointer Syntax ? Viper Library ??? ?? ?? GO ??? ????? ? ? ?? ???? ?? ? ???? ???? ?? ?????.
