Type System in Go
内置类型
基础类型
17 个基础类型(另加 2 个类型别名)是预声明类型(predeclared types)。[1]
- 字符串:
string
- 布尔类型:
bool
- 数值类型
int8
、uint8
(byte
)、int16
、uint16
、int32
(rune
)、uint32
、int64
、uint64
、int
、uint
、uintptr
float32
、float64
complex64
、complex128
byte
是 uint8
的内置别名;rune
是 int32
的内置别名。
复合类型
8 个复合类型。
- 指针类型(pointer)
- 结构体类型(struct)
- 函数类型(function)
- 容器类型
- 数组(array)
- 切片(slice)
- 字典(map):golang 编译器官方实现为哈希表
- 管道类型(channel)
- 接口 类型(interface)
unsafe pointer 类型
1 个 unsafe pointer 类型,由 golang 标准库 unsafe
包中引入。[2]
类型声明
类型声明(type declaration) 是创建自定义类型的语法糖。
类型定义声明
类型定义(type definition, or type definition declaration) 是创建新类型的方式。
type NewType SourceType
类型别名声明
类型别名声明(type alias declaration) 是创建类型别名的方式。
type NewType = SourceType