在 Go 中,没有像 C++ 中那样的引用类型。在 Go 中,一切都是按值传递的。当 Go 中使用术语“引用类型”时,它意味着引用它们应该表示的数据的类型(通过指针)。
切片是由类型表示的小型、类似结构的数据结构reflect.SliceHeader https://golang.org/pkg/reflect/#SliceHeader:
type SliceHeader struct {
Data uintptr
Len int
Cap int
}
它包含一个指向基础数组中切片的第一个元素的指针(SliceHeader.Data
场地)。该结构体很小,并且可以有效地作为值传递,无需传递其地址(并取消引用它以间接访问其任何字段)。切片的元素不存储在切片头中,而是存储在头内存区域之外的数组中。这意味着修改“指向”元素将修改原始切片的元素。
当您向切片追加(超过 0 个)元素时,Len
标头中的字段必须更改,因此描述具有附加元素的切片的新切片必须与追加之前的切片不同,这就是为什么您需要分配内置函数的返回值append()
功能。 (其他值也可能会改变,但是Len
肯定必须改变。)
映射被实现为指向runtime.hmap
结构:
type hmap struct {
// Note: the format of the hmap is also encoded in cmd/compile/internal/gc/reflect.go.
// Make sure this stays in sync with the compiler's definition.
count int // # live cells == size of map. Must be first (used by len() builtin)
flags uint8
B uint8 // log_2 of # of buckets (can hold up to loadFactor * 2^B items)
noverflow uint16 // approximate number of overflow buckets; see incrnoverflow for details
hash0 uint32 // hash seed
buckets unsafe.Pointer // array of 2^B Buckets. may be nil if count==0.
oldbuckets unsafe.Pointer // previous bucket array of half the size, non-nil only when growing
nevacuate uintptr // progress counter for evacuation (buckets less than this have been evacuated)
extra *mapextra // optional fields
}
正如您所看到的,这是一个比切片头复杂得多的数据结构,并且要大得多,将其作为值传递并不高效。
从映射中添加/删除元素(键值对)存储在该结构体的字段引用的存储桶中,但由于映射在底层被作为指针处理,因此您不需要分配此类操作的结果。
为了完整起见,通道也被实现为指针,指向runtime
包的hchan
type:
type hchan struct {
qcount uint // total data in the queue
dataqsiz uint // size of the circular queue
buf unsafe.Pointer // points to an array of dataqsiz elements
elemsize uint16
closed uint32
elemtype *_type // element type
sendx uint // send index
recvx uint // receive index
recvq waitq // list of recv waiters
sendq waitq // list of send waiters
// lock protects all fields in hchan, as well as several
// fields in sudogs blocked on this channel.
//
// Do not change another G's status while holding this lock
// (in particular, do not ready a G), as this can deadlock
// with stack shrinking.
lock mutex
}
这又是一个“胖”结构,并且像映射值一样处理。
查看相关问题:
参数中使用的切片与映射 https://stackoverflow.com/questions/47590444/slice-vs-map-to-be-used-in-parameter/47590531#47590531
使用值接收器附加到具有足够容量的切片 https://stackoverflow.com/questions/53009514/appending-to-a-slice-with-enough-capacity-using-value-receiver/53009770#53009770
golang切片是按值传递的吗? https://stackoverflow.com/questions/39993688/are-golang-slices-pass-by-value/39993797#39993797
Go 中的“值语义”和“指针语义”是什么意思? https://stackoverflow.com/questions/51264339/what-do-value-semantics-and-pointer-semantics-mean-in-go/51265000#51265000