- 自定义类型: type 自定义类型名 Go内置数据类型
例: type Myint int - 类型别名: type 自定义类型名 = Go内置数据类型
例: type Myint64 = int64
二者的区别:
- 自定义数据类型是一种新的数据类型,与其基于的Go内置数据类型 是两种数据类型,无法用来进行 运算符计算 (算数运算符、逻辑运算符、关系运算符、位运算符等)
- 类型别名 是其基于的Go内置数据类型的一种别名,二者是同一种数据类型,可以用来进行运算符计算
注: byte => type byte=uint8
rune => type rune = int32
type Myint int
var a Myint = 10
var b int = 10
if a==b {
fmt.Println("hello world")
}
程序报错:
invalid opeartion: a == b (mismatched types Myint and int)
type Myint64 int64
var a Myint64 = 10
var b int64 = 10
if a==b {
fmt.Println("hello world")
}
运行没有任何问题