类型转换 用于将一种数据类型 的变量 转换成另外一种类型的变量。
Go语言转换基本格式如下:
type_name(expression)
// 将 string 转换成 int
a. 使用 strconv.Atoi(stringvalue)
b. 使用strconv.ParseInt(s string, base int, bitSize int)
if base == 0, the base is implied by the string's prefix:
base 16 for "0x", base 8 for "0", and base 10 otherwise.
For bases 1, below 0 or above 36 an error is returned.
Bit sizes 0, 8, 16, 32, and 64 correspond to int, int8, int16, int32, and int64.
示例:
package main
import (
"fmt"
"strconv"
)
func main() {
var aa string = "1000"
var bb int = 5
var ccc float32
// 将 string 变为int
ee,_:= strconv.ParseInt(aa,10,64)
fmt.Println("ee",ee)
// 将 string 变为int
dd,_ := strconv.Atoi(aa)
fmt.Println(dd)
// 将 int 变为 string
mm :=strconv.Itoa(bb)
fmt.Println(mm)
ccc = float32(dd)/float32(bb)
}