我发现切片映射函数和通道经常作为 引用类型 一起被提及。但是我注意到切片的东西没有表现出任何参考行为,比如它们可能会过时:
var s []int
//must update slice value
s = append(s, ...)
要么
//must use pointer if we want to expose the change
func foo(s *[]int) error
//or change the function signature to return it like _append_
func foo(s []int) (r slice, err error)
通常我通过记住切片描述符实现的内部组件来理解这一点:切片值可以看作是 len、cap 和数据指针的结构。
但是地图值永远不需要像
m := make(map[string]int)
...
// don't know how to express with insertion, but you know what i mean.
m = delete(m, "well")
为什么?地图值只是指向地图描述符的指针吗?如果是这样,为什么不这样切片呢?
在 Go 中,没有像 C++ 中那样的引用类型。在 Go 中,一切都是按值传递的。在 Go 中使用术语“引用类型”时,它表示引用它们应该表示的数据的类型(通过指针)。
切片是由 type
表示的小型、类似结构的数据结构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 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
}
这又是一个“胖”结构,像地图值一样处理。
查看相关问题:
[在参数中使用的切片 vs 映射](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)