强制转化
type_name(expression)
例子
sum := 100
count :=6
// int
average1 := sum/count
// float32
average2 := float32(sum)/float32(count)
fmt.Printf("average1 type =%T, value=%d \n",average1,average1)
fmt.Printf("average2 type =%T, value=%f \n",average2,average2)
输出
average1 type =int, value=16
average2 type =float32, value=16.666666
自定义类型
格式
type $name struct {
$field $type;
}
例子
type User struct {
/** 首字母大写,允许其他包访问 */
Name string;
Province string;
/** 小写开头,不允许外面的包访问,本包可以使用*/
address string;
age int;
}
类型别名
type (
mystring string
)
例子
var str mystring;
str = "类别别名demo";
str2 := str +"!!!";
fmt.Printf("str type =%T \n",str)
fmt.Printf("str2 type =%T \n",str2)
var str3 string;
str3 = "GoLang";
fmt.Printf("str3 type =%T \n",str3)
输出
str type =main.mystring
str2 type =main.mystring
str3 type =string