Golang string to int strconv package

strconv is mainly used for the conversion of strings and basic types of data types

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

int to string type

s := strconv.Itoa(23)

int64 to string type

s := strconv.FormatInt(int64, 10)

string to int type

i, err := strconv.Atoi(s)

string to int64 type

s1 := "10000" //字符串
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 to bool type

s1 := "true" //字符串
b, err := strconv.ParseBool(s1)
if err != nil {
    
    
    fmt.Println(err) //打印错误信息
}
fmt.Printf("%T,%t", b, b) //bool,true

Guess you like

Origin blog.csdn.net/sunny_day_day/article/details/130639941