在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 enpred 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}这又是一个“胖”结构,其处理方式类似于地图值。



