Links
Comment on page

4.4.4-4 内置复合类型

除了基础类型之外,Go 语言规范中还定义了多种复合类型,编译器为每种类型都定义了对应的数据结构

4.4.4-4.1. 数组

数组类型(Array)的结构定义如下:
type Array struct {
len int64
elem Type
}
数组是定长的数据容器,所以除了元素类型之外,数组长度也是其类型中的组成部分,因此[2]int[3]int虽然都是整型数组,但对类型系统而言,他们属于不同的类型。

4.4.4-4.2. 切片

切片类型(Slice)的结构定义如下:
type Slice struct {
elem Type
}
切片是变长的数据容器,其类型定义中不包括长度信息。

4.4.4-4.3. Map

Map 类型的结构定义如下:
type Map struct {
key, elem Type
}
其中key必须是可比较的数据类型(Comparable Type),后文中类型的比较规则有详细介绍。

4.4.4-4.4. Channel

Channel 类型的结构定义如下:
type Chan struct {
dir ChanDir
elem Type
}
// A ChanDir value indicates a channel direction.
type ChanDir int
// The direction of a channel is indicated by one of these constants.
const (
SendRecv ChanDir = iota
SendOnly
RecvOnly
)
其中一个是 channel 的元素类型,一个是 channel 的数据流方向。

4.4.4-4.5. 指针

指针类型的结构定义如下:
type Pointer struct {
base Type // element type
}