类型转换
1.interface 转 string 或则其他
func interfaceToString(inter interface{}) {
switch inter.(type) {
case string:
fmt.Println("string", inter.(string))
break
case int:
fmt.Println("int", inter.(int))
break
case float64:
fmt.Println("float64", inter.(float64))
break
}
}
// 扩展:
username := (viper.Get("mysql.username")).(string)
password := (viper.Get("mysql.password")).(string)
host := (viper.Get("mysql.host")).(string)
port := (viper.Get("mysql.port")).(int64)
database := (viper.Get("mysql.database")).(string)
2.int64 转 string
dataSource.WriteString(strconv.FormatInt(port, 10))
2. int64 转 string
strconv.FormatInt(port, 10)
3.int 转 string
str:=strconv.Itoa(123)
fmt.Println(str,reflect.TypeOf(str))
4. string 转 int
int,err:=strconv.Atoi("123")
if err!=nil {
panic(err)
}
fmt.Println(num,reflect.TypeOf(num))