我发现 slice 映射函数和 channel 经常作为引用类型一起提到。但是我注意到 slice 的东西没有表现出引用行为,就像它们可能会过时一样:
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)
通常我通过牢记 slice 描述符实现的内部组件来理解这一点: slice 值可以看作是 len、cap 和数据指针的结构。
但是 map 值永远不需要像这样打扰
m := make(map[string]int)
...
// don't know how to express with insertion, but you know what i mean.
m = delete(m, "well")
为什么? map 值只是指向 map 描述符的指针吗?如果是这样,为什么不也用这种方式制作 slice 呢?
最佳答案
在 Go 中没有像 C++ 中那样的引用类型。在 Go 中,一切都是按值传递的。当术语“引用类型”在 Go 中使用时,它表示引用它们应该表示的数据的类型(通过指针)。
slice 是小型的、类似结构的数据结构,由 reflect.SliceHeader
类型表示:
type SliceHeader struct {
Data uintptr
Len int
Cap int
}
它包含指向底层数组(SliceHeader.Data
字段)中 slice 第一个元素的指针。这个结构体很小,作为值传递很有效,不需要传递它的地址(并取消引用它以间接访问它的任何字段)。 slice 的元素不存储在 slice 头中,而是存储在头内存区域之外的数组中。这意味着修改“尖头”元素将修改原始 slice 的元素。
当您将(超过 0 个)元素附加到 slice 时, header 中的 Len
字段必须更改,因此描述具有附加元素的 slice 的新 slice 必须不同于追加之前的一个,这就是为什么您需要分配内置 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
}
如您所见,这是比 slice header 复杂得多的数据结构,而且要大得多,将其作为值传递效率不高。
从映射中添加/删除元素(键值对)存储在该结构的字段引用的桶中,但由于映射在引擎盖下作为指针处理,因此您不需要分配结果这样的操作。
为了完整起见, channel 也被实现为指针,指向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
}
这又是一个“胖”结构,处理方式与映射值类似。
查看相关问题:
slice vs map to be used in parameter
Appending to a slice with enough capacity using value receiver
Are golang slices pass by value?
What do "value semantics’" and "pointer semantics" mean in Go?
关于dictionary - 为什么 slice 值有时会过时但永远不会映射值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55520624/