string 和 int转换
// string 转 ints1 := "100"i1, err := strconv.Atoi(s1)if err != nil { fmt.Printf("类型转换错误: %s", err.Error()) return}s2 := strconv.Itoa(i1)fmt.Printf("s1 类型是:%T , i1类型是:%T , s2类型是:%T", s1, i1, s2)
转换字符串为定类型值
// string 转 bool b1, _ := strconv.ParseBool("true") // string 转 int // base : 指定进制 (2,8,10,16) // bitSize : 指定int类型 (最大位) i1, _ := strconv.ParseInt("255", 10, 32) // ... fmt.Printf("b1 类型是: %T , i1 类型是:%T", b1, i1)
指定类型数据格式化为string类型
// bool 转strings1 := strconv.FormatBool(true)s2 := strconv.FormatInt(908, 10)// TODO: 异常,稍后研究s3 := strconv.FormatFloat(1.2, 'e', -1, 10)fmt.Printf("s1 , s2 ,s3 types : %T %T %T", s1, s2, s3)