栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

为什么切片值有时会过时但不能映射值?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

为什么切片值有时会过时但不能映射值?

在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}

这又是一个“胖”结构,其处理方式类似于地图值。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/484319.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号