[英]why slice values can sometimes go stale but never map values?
我發現切片圖功能和通道經常一起作為參考類型提及。 但是我注意到,切片的東西不會表現出任何參考行為,因為它們可能會過時:
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和data指針的結構。
但是地圖值永遠不需要像
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中使用術語“引用類型”時,它表示引用它們應表示的數據(通過指針)的類型。
切片是小型的,類似於結構的數據結構,由類型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
}
如您所見,這是一個比切片標頭復雜得多的數據結構,並且要大得多,將其作為值傳遞將是無效的。
從映射中添加/刪除元素(鍵-值對)存儲在此struct的字段所引用的存儲桶中,但是由於映射在幕后被當作指針處理,因此您無需分配此類操作的結果。
為了完整hchan
,通道也被實現為指針,指向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是連續內存塊上的薄紙包裝,通常有益於部分或全部重用該內容(避免復制數據)。 地圖沒有這些特征。 這是一個具有復雜行為的復雜數據結構,您無法重復使用其存儲(就像對切片一樣)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.