在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
类型转换用于将一种数据类型的变量转换为另外一种类型的变量。 Go语言类型转换基本格式如下:
Go语言各种类型转换及函数的高级用法:
转字节reflect.TypeOf() 查看类型
字符串转字节 package main import ( "fmt" "reflect" ) func main() { var str string = "oldboy" result := []byte(str) fmt.Printf("result is %v , result type is %v\n", result, reflect.TypeOf(result)) } 32位整形转字节 package main import ( "bytes" "encoding/binary" "fmt" "reflect" ) 转字符串字节转字符串 package main import ( "fmt" "reflect" ) func main() { var b []byte = []byte{111, 108, 100, 98, 111, 121} result := string(b) fmt.Printf("result is %v , result type is %v\n", result, reflect.TypeOf(result)) } 整形转字符串 strconv.Itoa(x) package main import ( "fmt" "reflect" "strconv" ) func main() { var x int x = 100 result := strconv.Itoa(x) fmt.Printf("result is %v , result type is %v\n", result, reflect.TypeOf(result)) } FormatInt 将 int 型整数 i 转换为字符串形式 func FormatInt(i int64, base int) string Itoa 相当于 FormatInt(i, 10) 64位整形转字符串 package main import ( "fmt" "reflect" "strconv" ) func main() { var i int64 i = 0x100 result := strconv.FormatInt(i, 10) fmt.Printf("result is %v , result type is %v\n", result, reflect.TypeOf(result)) } 布尔值转字符串 package main import ( "fmt" "reflect" "strconv" ) func main() { t := strconv.FormatBool(true) f := strconv.FormatBool(false) fmt.Printf("t is %v , t type is %v\n", t, reflect.TypeOf(t)) fmt.Printf("f is %v , f type is %v\n", f, reflect.TypeOf(f)) } 浮点数转字符串 strconv.FormatFloat(f,fmt,prec,bitSize) f:要转换的浮点数 fmt:格式标记(b、e、E、,f、g、G) prec:精度(数字部分的长度,不包括指数部分) bitSize:指定浮点类型(32:float32、64:float64) 格式标记: ‘b’ (-ddddp±ddd,二进制指数) ‘e’ (-d.dddde±dd,十进制指数) ‘E’ (-d.ddddE±dd,十进制指数) ‘f’ (-ddd.dddd,没有指数) ‘g’ (‘e’:大指数,’f’:其它情况) ‘G’ (‘E’:大指数,’f’:其它情况) package main import ( "fmt" "reflect" "strconv" ) func main() { f := 100.12345678901234567890123456789 result := strconv.FormatFloat(f, 'e', 30, 32) fmt.Printf("result is %v , result type is %v\n", result, reflect.TypeOf(result)) } 转整形int转int64 package main import ( "fmt" "reflect" ) func main() { var x int = 100 result := int64(x) fmt.Printf("result is %v , result type is %v\n", result, reflect.TypeOf(result)) } 字节转32位整形 package main import ( "bytes" "encoding/binary" "fmt" "reflect" ) func main() { b := []byte{0x00, 0x00, 0x03, 0xe8} bytesBuffer := bytes.NewBuffer(b) var result int32 binary.Read(bytesBuffer, binary.BigEndian, &result) fmt.Printf("result is %v , result type is %v\n", result, reflect.TypeOf(result)) } 转浮点型float32转float64 package main import ( "fmt" "reflect" ) func main() { var x float32 = 100 result := float64(x) fmt.Printf("result is %v , result type is %v\n", result, reflect.TypeOf(result)) } 整型转浮点型 package main import ( "fmt" "reflect" ) func main() { var x int = 100 result := float32(x) fmt.Printf("result is %v , result type is %v\n", result, reflect.TypeOf(result)) } 字符串转浮点数 strconv.ParseFloat(str,bitSize) str:要转换的字符串 bitSize:指定浮点类型(32:float32、64:float64) 如果 str 是合法的格式,而且接近一个浮点值, 则返回浮点数的四舍五入值(依据 IEEE754 的四舍五入标准) 如果 str 不是合法的格式,则返回“语法错误” 如果转换结果超出 bitSize 范围,则返回“超出范围” package main import ( "fmt" "reflect" "strconv" ) func main() { var str string = "0.12345678901234567890" result, _ := strconv.ParseFloat(str, 32) fmt.Printf("result is %v , result type is %v\n", result, reflect.TypeOf(result)) } 转布尔值字符串转布尔值
注意:Go语言不能将其他类型当 bool 值使用 package main func main() { a := 100 if a { } } 编译错误: ./main.go:5:2: non-bool a (type int) used as if condition
|
请发表评论