1.int整數轉字符串
1.1fmt.Sprintf
1.2strconv.Itoa
1.3strconv.FormatIntgit
2.字符串轉int整數
2.1strconv.Atoi
2.2strconv.ParseInt學習
fmt 包應該是最多見的了,從剛開始學習 Golang 就接觸到了,寫 ‘hello, world' 就得用它。它還支持格式化變量轉爲字符串。%d 表明十進制整數。spa
//Sprintf formats according to a format specifier and returns the resulting string.
func Sprintf(format string, a ...interface{}) string 複製代碼
使用示例:code
str := fmt.Sprintf("%d", a)
複製代碼
支持 int 類型轉換成字符串orm
//Itoa is shorthand for FormatInt(int64(i), 10).
func Itoa(i int) string 複製代碼
使用示例:ci
str := strconv.Itoa(a)
複製代碼
支持 int64 類型轉換成字符串 參數 i 是要被轉換的整數, base 是進制,例如2進制,支持2到36進制。字符串
//FormatInt returns the string representation of i in the given base, for 2 <= base <= 36. The result uses the lower-case letters ‘a' to ‘z' for digit values >= 10.
func FormatInt(i int64, base int) string 複製代碼
使用示例:string
str := strconv.FormatInt(a, 10)
複製代碼
比較常見的方法it
// Atoi returns the result of ParseInt(s, 10, 0) converted to type int.
func Atoi(s string) (int, error) 複製代碼
使用示例:io
i,err := strconv.Atoi(a)
複製代碼
功能很是強大
// ParseInt interprets a string s in the given base (0, 2 to 36) and
// bit size (0 to 64) and returns the corresponding value i.
func ParseInt(s string, base int, bitSize int) (i int64, err error) 複製代碼
參數1 數字的字符串形式 參數2 數字字符串的進制 好比二進制 八進制 十進制 十六進制 參數3 返回結果的bit大小 也就是int8 int16 int32 int64
使用示例:
i, err := strconv.ParseInt("123", 10, 32)
複製代碼