go语言中map、slice、chan底层数据布局是怎样的

打印 上一主题 下一主题

主题 536|帖子 536|积分 1608

map:rutime包下的map中的hmap
  1. // A header for a Go map.
  2. type hmap struct {
  3.         // Note: the format of the hmap is also encoded in cmd/compile/internal/reflectdata/reflect.go.
  4.         // Make sure this stays in sync with the compiler's definition.
  5.         count     int // # live cells == size of map.  Must be first (used by len() builtin)
  6.         flags     uint8
  7.         B         uint8  // log_2 of # of buckets (can hold up to loadFactor * 2^B items)
  8.         noverflow uint16 // approximate number of overflow buckets; see incrnoverflow for details
  9.         hash0     uint32 // hash seed
  10.         buckets    unsafe.Pointer // array of 2^B Buckets. may be nil if count==0.
  11.         oldbuckets unsafe.Pointer // previous bucket array of half the size, non-nil only when growing
  12.         nevacuate  uintptr        // progress counter for evacuation (buckets less than this have been evacuated)
  13.         extra *mapextra // optional fields
  14. }
复制代码
slice:runtime包下的slice中的slice
  1. type slice struct {
  2.         array unsafe.Pointer
  3.         len   int
  4.         cap   int
  5. }
复制代码
chan: runtime包下的chan中的hchan
  1. type hchan struct {
  2.         qcount   uint           // total data in the queue
  3.         dataqsiz uint           // size of the circular queue
  4.         buf      unsafe.Pointer // points to an array of dataqsiz elements
  5.         elemsize uint16
  6.         closed   uint32
  7.         elemtype *_type // element type
  8.         sendx    uint   // send index
  9.         recvx    uint   // receive index
  10.         recvq    waitq  // list of recv waiters
  11.         sendq    waitq  // list of send waiters
  12.         // lock protects all fields in hchan, as well as several
  13.         // fields in sudogs blocked on this channel.
  14.         //
  15.         // Do not change another G's status while holding this lock
  16.         // (in particular, do not ready a G), as this can deadlock
  17.         // with stack shrinking.
  18.         lock mutex
  19. }
复制代码



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

罪恶克星

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表