go strconv模块
作者:互联网
package main
import (
"fmt"
"strconv"
)
func main() {
// 把数字转换成字符串类型
i1 := int32(97)
fmt.Printf("i1 = %d, %T, %#v,%#v \n",i1,i1,i1,string(i1))
ret2 := fmt.Sprintf("%d",i1)
fmt.Printf("ret2 = %d, %T, %#v \n",ret2, ret2, ret2)
// 把字符串类型解析成数字
str := "10000"
ret1,err := strconv.ParseInt(str, 10, 64)
if err != nil {
fmt.Println("parseing failed, err: ",err)
return
}
fmt.Printf("%#v %T\n",ret1,ret1)
retInt ,_ := strconv.Atoi(str) // go里面字符串是Array类型的字节,所以是Atoi,而不是Stoi
fmt.Printf("ret2 = %d, %T, %#v \n",retInt, retInt, retInt)
i := 97
ret3 := strconv.Itoa(i)
fmt.Printf("ret3 = %s, %T, %#v \n",ret3, ret3, ret3)
// Atoi:字符串转换成int
retInt, _ = strconv.Atoi(str)
fmt.Printf("retInt %#v %T\n", retInt, retInt)
//从字符串中解析出布尔值
boolStr := "true"
boolValue,_ := strconv.ParseBool(boolStr)
fmt.Printf("boolStr %#v %T\n", boolValue,boolValue)//从字符串中解析出浮点数
floatStr := "1.234"
floatValue,_:= strconv.ParseFloat(floatStr,64)
fmt.Printf("floatStr %#v %T\n",floatValue,floatValue)
}
输出:
i1 = 97, int32, 97,"a"
ret2 = %!d(string=97), string, "97"
10000 int64
ret2 = 10000, int, 10000
ret3 = 97, string, "97"
retInt 10000 int
boolStr true bool
floatStr 1.234 float64
标签:i1,fmt,strconv,%#,97,模块,go,retInt 来源: https://www.cnblogs.com/heris/p/16121142.html