Go里的字符串与数字互相转换主要使用strconv
包
数字转字符串
package main
import "strconv"
func main () {
s := strconv.Itoa(-32)
}
字符串转数字
假定要转换为int
类型
package main
import "strconv"
func main () {
i, err := strconv.Atoi("-32")
if err != nil {
...
}
}
当然,也要注意长度,其中有一句话
The parse functions return the widest type (float64, int64, and uint64), but if the size argument specifies a narrower width the result can be > converted to that narrower type without data loss:
...