go string转int strconv包

前言

strconv 主要用于字符串和基本类型的数据类型的转换

s := "aa"+100
//字符串和整形数据不能放在一起  所以需要将 100 整形转为字符串类型 
//+号在字符串中表示字符串的连接 在整形中表示数据的计算

int 转 string 类型

s := strconv.Itoa(23)

int64 转 string 类型

s := strconv.FormatInt(int64, 10)

string 转 int 类型

i, err := strconv.Atoi(s)

string 转 int64 类型

s1 := "100" //字符串
b, err := strconv.ParseInt(s1, 10, 64)
//10 表示s1要转的数据是10进制 64位
if err != nil {
    fmt.Println(err) //打印错误信息
}
fmt.Printf("%T,%d", b, b) //int64,100

string 转 bool类型

s1 := "true" //字符串
b, err := strconv.ParseBool(s1)
if err != nil {
    fmt.Println(err) //打印错误信息
}
fmt.Printf("%T,%t", b, b) //bool,true
posted @ 2020-11-09 16:35  牛奔  阅读(129)  评论(4编辑  收藏  举报