golang 常用数据类型转换

str <-> int

num := 123
num2Str := strconv.Itoa(num)
fmt.Printf("%T", num2Str)
str := "12345"
str2Int, _ := strconv.Atoi(str)
fmt.Printf("%T", str2Int)

str <-> float

var strFloat = "1.23"
str2Float, _ := strconv.ParseFloat(strFloat, 64)
fmt.Printf("%T", str2Float)
float1 := 9.99
float12Str := strconv.FormatFloat(float1, 'f', 10, 64)
fmt.Printf("%T", float12Str)

str <-> byte || rune

byteChar := 'a'
byteChar2Str := string(byteChar)
fmt.Printf("%T %q", byteChar2Str, byteChar2Str)
strChar := "a"
strChar2Byte := []byte(strChar)[0]
fmt.Printf("%T", strChar2Byte)

posted on 2022-01-18 17:09  进击的davis  阅读(82)  评论(0编辑  收藏  举报

导航