速记
// 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)
详解
int to string
strconv.Itoaint
s := strconv.Itoa(97) // s == "97"
string()string(int)
s := string(97) // s == "a"
int64 to string
strconv.FormatInt
toStringtoString
var n int64 = 97
s := strconv.FormatInt(n, 10) // s == "97" (decimal)
var n int64 = 97
s := strconv.FormatInt(n, 16) // s == "61" (16 进制,即hex转码)
string to int
strconv.Atois22invalid syntax
s := "97"
if n, err := strconv.Atoi(s); err == nil {
fmt.Println(n+1)
} else {
fmt.Println(s, "is not an integer.")
}
// Output: 98
string to int64
strconv.ParseInt
s := "97"
n, err := strconv.ParseInt(s, 10, 64)
if err == nil {
fmt.Printf("%d of type %T", n, n)
}
// Output: 97 of type int64
第二个参数,表示数字字符串的进制,如二进制,八进制,十进制等
第三个参数,返回结果的bit大小, 如0, 8, 16, 32, 64,分别转化为 int,int8, int16, int32, int64,每种int都有各自的范围,超出范围会报错
参考文档