Go string轉(zhuǎn)int,int64,int32及注意事項說明
更新時間:2024年07月29日 16:06:34 作者:斜杠打卡小程序
這篇文章主要介紹了Go string轉(zhuǎn)int,int64,int32及注意事項說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
Go string轉(zhuǎn)int,int64,int32及注意事項
string 轉(zhuǎn) int (正整數(shù))
// string 轉(zhuǎn) int str5 := "123" int5, err := strconv.Atoi(str5) if err != nil { fmt.Println(err) } else { fmt.Printf("int5 value is %d, type is %T\n", int5, int5) // int5 value is 123, type is int }
string 轉(zhuǎn) int (負(fù)整數(shù))
// string 轉(zhuǎn) int str6 := "-123" int6, err := strconv.Atoi(str6) if err != nil { fmt.Println(err) } else { fmt.Printf("int6 value is %d, type is %T\n", int6, int6) // int6 value is -123, type is int }
string 轉(zhuǎn) int64
// string 轉(zhuǎn) int64 str7 := "123" int7, err := strconv.ParseInt(str7, 10, 64) if err != nil { fmt.Println(err) } else { fmt.Printf("int7 value is %d, type is %T\n", int7, int7) // int7 value is 123, type is int }
string 轉(zhuǎn) int64
// string 轉(zhuǎn) int64 str9 := "123" // 參數(shù)1 數(shù)字的字符串形式 // 參數(shù)2 數(shù)字字符串的進(jìn)制 比如二進(jìn)制 八進(jìn)制 十進(jìn)制 十六進(jìn)制 // 參數(shù)3 返回結(jié)果的bit大小 也就是int8 int16 int32 int64 int9, err := strconv.ParseInt(str9, 8, 64) if err != nil { fmt.Println(err) } else { fmt.Printf("int9 value is %d, type is %T\n", int9, int9) // int9 value is 83, type is int64 // 83 = 1 * 8^2 + 2 * 8^1 + 3 * 8^0 }
string 轉(zhuǎn) int32
// string 轉(zhuǎn) int32 str10 := "123" int10, err := strconv.ParseInt(str10, 10, 32) if err != nil { fmt.Println(err) } else { j := int32(int10) fmt.Printf("j value is %d, type is %T", j, j) // j value is 123, type is int32% } }
string 轉(zhuǎn) 數(shù)值類型 必須為數(shù)字的字符串形式
// string 轉(zhuǎn) 數(shù)值類型 必須為數(shù)字的字符串形式 str1 := "123sd" int1, err := strconv.Atoi(str1) if err != nil { fmt.Println(err) // strconv.Atoi: parsing "123sd": invalid syntax } else { fmt.Printf("int1 value is %d, type is %T\n", int1, int1) }
string 轉(zhuǎn) 數(shù)值類型 必須為數(shù)字的字符串形式
// string 轉(zhuǎn) 數(shù)值類型 必須為數(shù)字的字符串形式 str2 := "123sd123" int2, err := strconv.Atoi(str2) if err != nil { fmt.Println(err) // strconv.Atoi: parsing "123sd123": invalid syntax } else { fmt.Printf("int2 value is %d, type is %T\n", int2, int2) }
string 轉(zhuǎn) 數(shù)值類型 數(shù)值范圍必須在目標(biāo)范圍內(nèi)
// string 轉(zhuǎn) 數(shù)值類型 數(shù)值范圍必須在目標(biāo)范圍內(nèi) str3 := "11111111111111111111" int3, err := strconv.Atoi(str3) if err != nil { fmt.Println(err) // strconv.Atoi: parsing "11111111111111111111": value out of range } else { fmt.Printf("int3 value is %d, type is %T\n", int3, int3) }
string 轉(zhuǎn) 數(shù)值類型 浮點數(shù)無法轉(zhuǎn)整型
// string 轉(zhuǎn) 數(shù)值類型 浮點數(shù)無法轉(zhuǎn)整型 str4 := "123.12" int4, err := strconv.Atoi(str4) if err != nil { fmt.Println(err) // strconv.Atoi: parsing "123.12": invalid syntax } else { fmt.Printf("int4 value is %d, type is %T\n", int4, int4) }
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
淺談Go語言中的結(jié)構(gòu)體struct & 接口Interface & 反射
下面小編就為大家?guī)硪黄獪\談Go語言中的結(jié)構(gòu)體struct & 接口Interface & 反射。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07go語言中的udp協(xié)議及TCP通訊實現(xiàn)示例
這篇文章主要為大家介紹了go語言中的udp協(xié)議及TCP通訊的實現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04Go語言實現(xiàn)二進(jìn)制與十進(jìn)制互轉(zhuǎn)的示例代碼
這篇文章主要和大家詳細(xì)介紹了Go語言中實現(xiàn)二進(jìn)制與十進(jìn)制互相轉(zhuǎn)換的示例代碼,文中的代碼簡潔易懂,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-05-05Go語言的文件名、標(biāo)識符、關(guān)鍵字和包基礎(chǔ)教程
Go的關(guān)鍵字不能被用作標(biāo)識符,這是一個重要的限制,以避免命名沖突和語法混淆,這篇文章主要給大家介紹了關(guān)于Go語言文件名、標(biāo)識符、關(guān)鍵字和包的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06