T : 就是数据类型
v : 就是需要转换的变量
数值间转换的时候建议从小范围转换成大范围,比如int8转int16,大范围转换成小范围的时候,比如int16转int8,会发生精度丢失(截断)的情况,如果转换不成功就会溢出。
var a int8 = 6
var b int16 = 12
// var c = a + b //invalid operation: a + b (mismatched types int8 and int16)
var c = int16(a) + b
fmt.Printf("整形间的数值运算,值:%v --- 类型%T \n", c, c)
var d = a + int8(b)
fmt.Printf("整形间的数值运算,值:%v --- 类型%T \n", d, d) // 可能会溢出,不建议
//结果如下:
整形间的数值运算,值:18 --- 类型int16
整形间的数值运算,值:18 --- 类型int8
var a float32 = 6.1
var b float64 = 12.1
var c = float64(a) + b
fmt.Printf("浮点类型间的数值运算,值:%v --- 类型%T \n", c, c)
// 结果如下:
浮点类型间的数值运算,值:18.19999990463257 --- 类型float64
var a int8 = 6
var b float64 = 12.1
var c = float64(a) + b
fmt.Printf("整数与浮点类型间的数值运算,值:%v --- 类型%T \n", c, c)
// 结果如下:
整数与浮点类型间的数值运算,值:18.1 --- 类型float64
fmt.Sprintf()
把其他类型转换成String类型注意:fmt.Sprintf()使用中需要注意转换的格式,int 为%d, float 为%f, bool为%t, byte 为%c
var a int = 20
var b float64 = 12.456
var c bool = true
var d byte = 'a'
var stra, strb, strc, strd string
stra = fmt.Sprintf("%d", a)
strb = fmt.Sprintf("%f", b)
strc = fmt.Sprintf("%t", c)
strd = fmt.Sprintf("%c", d)
fmt.Printf("stra type is %T, value is %v \n", stra, stra)
fmt.Printf("strb type is %T, value is %v \n", strb, strb)
fmt.Printf("strc type is %T, value is %v \n", strc, strc)
fmt.Printf("strd type is %T, value is %v \n", strd, strd)
// 结果如下:
stra type is string, value is 20
strb type is string, value is 12.456000
strc type is string, value is true
strd type is string, value is a
strconv
包把其他类型转换成String类型var a int64 = 20
var stra string = ""
stra=strconv.FormatInt(a,10)
fmt.Printf("stra type is %T, value is %v \n", stra, stra)
var b int = 20
strb := strconv.Itoa(b) // strconv.Itoa函数等效于strconv.FormatInt(int64(i), 10)
fmt.Printf("strb type is %T, value is %v \n", strb, strb)
// 结果如下:
stra type is string, value is 20
strb type is string, value is 20
strconv.Itoa()
等价于strconv.FormatInt()
strconv.FormatFloat(f float64, fmt byte, prec, bitSize int)
参数1是要转换的值,需要时float64类型
参数2是格式化的进制类型
'f' (-ddd.dddd)
'b' (-ddddp±ddd,指数为二进制)
'e'(-d.dddde±dd,十进制指数)、
'E'(-d.ddddE±dd,十进制指数)、
'g' (指数很大时用'e'格式,否则'f'格式)
'G' (指数很大时用'E'格式,否则'f'格式)
参数3是保留的小数点数量,设置为 -1 表示不对小数点格式化 ,设置为 3 表示保留三位小数。
参数4是格式化后的类型为int,位数是设置的bitSize,设置64,那就是格式化后的具体类型为int64。
var a float64 = 20.123456
str01 := strconv.FormatFloat(a, 'f', -1, 64)
str02 := strconv.FormatFloat(a, 'f', 3, 64)
str03 := strconv.FormatFloat(a, 'b', 3, 64)
str04 := strconv.FormatFloat(a, 'e', 3, 64)
str05 := strconv.FormatFloat(a, 'E', 3, 64)
str06 := strconv.FormatFloat(a, 'g', 3, 64)
str07 := strconv.FormatFloat(a, 'G', 3, 64)
fmt.Printf("str01 type %T, str01=%v \n", str01, str01)
fmt.Printf("str02 type %T, str02=%v \n", str02, str02)
fmt.Printf("str03 type %T, str03=%v \n", str03, str03)
fmt.Printf("str04 type %T, str04=%v \n", str04, str04)
fmt.Printf("str05 type %T, str05=%v \n", str05, str05)
fmt.Printf("str06 type %T, str06=%v \n", str06, str06)
fmt.Printf("str07 type %T, str07=%v \n", str07, str07)
// 结果如下:
str01 type string, str01=20.123456
str02 type string, str02=20.123
str03 type string, str03=5664249308937911p-48
str04 type string, str04=2.012e+01
str05 type string, str05=2.012E+01
str06 type string, str06=20.1
str07 type string, str07=20.1
var a bool = true
str := strconv.FormatBool(a)
fmt.Printf("str type %T, str=%v \n", str, str)
var b bool = false
str01 := strconv.FormatBool(b)
fmt.Printf("str01 type %T, str01=%v \n", str01, str01)
// 结果如下:
str type string, str=true
str01 type string, str01=false
Parse类函数用于转换字符串为给定类型的值:ParseBool()
、ParseFloat()
、ParseInt()
、 ParseUint()
。
由于字符串转换为其它类型可能会失败,所以这些函数都有两个返回值,第一个返回值保存 转换后的值,第二个返回值判断是否转换成功。
var a string = "12"
num, err := strconv.ParseInt(a, 10, 64)
fmt.Printf("String类型转成数值类型,类型是: %T, 值是:%v, %v \n", num, num, err)
var b string = "hello"
num01, err01 := strconv.ParseInt(b, 10, 64)
fmt.Printf("String类型转成数值类型,类型是: %T, 值是:%v, %v \n", num01, num01, err01)
var c string = "12.1"
num02, err02 := strconv.ParseInt(c, 10, 64)
fmt.Printf("String类型转成数值类型,类型是: %T, 值是:%v, %v \n", num02, num02, err02)
var a string = "-12"
num, err := strconv.ParseInt(a, 10, 64)
num01, err01 := strconv.ParseUint(a, 10, 64)
fmt.Printf("String类型转成Int类型,类型是: %T, 值是:%v, %v \n", num, num, err)
fmt.Printf("String类型转成Int类型,类型是: %T, 值是:%v, %v \n", num01, num01, err01)
var b string = "12"
num02, err02 := strconv.ParseInt(b, 10, 64)
num03, err03 := strconv.ParseUint(b, 10, 64)
fmt.Printf("String类型转成Int类型,类型是: %T, 值是:%v, %v \n", num02, num02, err02)
fmt.Printf("String类型转成Int类型,类型是: %T, 值是:%v, %v \n", num03, num03, err03)
var a string = "-12"
num, err := strconv.Atoi(a)
fmt.Printf("String类型转成Int类型,类型是: %T, 值是:%v, %v \n", num, num, err)
var b string = "12"
num01, err01 := strconv.Atoi(b)
fmt.Printf("String类型转成Int类型,类型是: %T, 值是:%v, %v \n", num01, num01, err01)
// 结果如下:
String类型转成数值类型,类型是: int64, 值是:12, <nil>
String类型转成数值类型,类型是: int64, 值是:0, strconv.ParseInt: parsing "hello": invalid syntax
String类型转成数值类型,类型是: int64, 值是:0, strconv.ParseInt: parsing "12.1": invalid syntax
String类型转成Int类型,类型是: int64, 值是:-12, <nil>
String类型转成Int类型,类型是: uint64, 值是:0, strconv.ParseUint: parsing "-12": invalid syntax
String类型转成Int类型,类型是: int64, 值是:12, <nil>
String类型转成Int类型,类型是: uint64, 值是:12, <nil>
String类型转成Int类型,类型是: int, 值是:-12, <nil>
String类型转成Int类型,类型是: int, 值是:12, <nil>
非数字字符串不可以转换为int类型,浮点字符串也不可以转换为int类型
ParseInt()
可以转换正数和负数,ParseUint()
只能转换正数
strconv.Atoi()
等价于ParseInt()
var a string = "12.123456"
num, err := strconv.ParseFloat(a, 64)
fmt.Printf("String类型转成float类型,类型是: %T, 值是:%v, %v \n", num, num, err)
var b string = "12"
num01, err01 := strconv.ParseFloat(b, 64)
fmt.Printf("String类型转成float类型,类型是: %T, 值是:%v, %v \n", num01, num01, err01)
var c string = "hello"
num02, err02 := strconv.ParseFloat(c, 64)
fmt.Printf("String类型转成float类型,类型是: %T, 值是:%v, %v \n", num02, num02, err02)
// 结果如下:
String类型转成float类型,类型是: float64, 值是:12.123456, <nil>
String类型转成float类型,类型是: float64, 值是:12, <nil>
String类型转成float类型,类型是: float64, 值是:0, strconv.ParseFloat: parsing "hello": invalid syntax
非数字字符串不可以转换为float类型,int字符串也不可以转换为float类型
var a string = "true"
bo1, err := strconv.ParseBool(a)
fmt.Printf("String类型转成bool类型,类型是: %T, 值是:%v, %v \n", bo1, bo1, err)
var b string = "false"
bo2, err01 := strconv.ParseBool(b)
fmt.Printf("String类型转成bool类型,类型是: %T, 值是:%v, %v \n", bo2, bo2, err01)
var c string = "hello"
bo3, err02 := strconv.ParseBool(c)
fmt.Printf("String类型转成bool类型,类型是: %T, 值是:%v, %v \n", bo3, bo3, err02)
var d string = "12"
bo4, err03 := strconv.ParseBool(d)
fmt.Printf("String类型转成bool类型,类型是: %T, 值是:%v, %v \n", bo4, bo4, err03)
// 结果如下:
String类型转成bool类型,类型是: bool, 值是:true, <nil>
String类型转成bool类型,类型是: bool, 值是:false, <nil>
String类型转成bool类型,类型是: bool, 值是:false, strconv.ParseBool: parsing "hello": invalid syntax
String类型转成bool类型,类型是: bool, 值是:false, strconv.ParseBool: parsing "12": invalid syntax
只有字符串"true"和"false"可以转换为bool类型,其他不可以
1、https://www.cnblogs.com/ludundun/p/16938407.html
2、https://blog.csdn.net/tianyi19/article/details/112776530
3、http://www.js-code.com/golang/golang_221566.html