Comment on page
4.4.4-4 内置复合类型
除了基础类型之外,Go 语言规范中还定义了多种复合类型,编译器为每种类型都定义了对应的数据结构
数组类型(Array)的结构定义如下:
type Array struct {
len int64
elem Type
}
数组是定长的数据容器,所以除了元素类型之外,数组长度也是其类型中的组成部分,因此
[2]int
与[3]int
虽然都是整型数组,但对类型系统而言,他们属于不同的类型。切片类型(Slice)的结构定义如下:
type Slice struct {
elem Type
}
切片是变长的数据容器,其类型定义中不包括长度信息。
Map 类型的结构定义如下:
type Map struct {
key, elem Type
}
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 的数据流方向。
指针类型的结构定义如下:
type Pointer struct {
base Type // element type
}