握月担风
  • 标签🏷️
主页 » 🧩 标签

源码解析

golang-map介绍

本文中使用的 go 版本: go version go1.17.2 darwin/amd64 内容概述 本文介绍 golang 中经常用到的结构-map,简称哈希表、字典。介绍其结构及设计思路。 map 在源码中的结构——hmap Go 语言采用的核心数据结构是哈希查找表,使用链表解决哈希冲突。 在源码中$GOROOT/src/runtime/map.go,map 的核心结构体是这样的: // A header for a Go map. type hmap struct { count int // map中的元素数量,即len(map)时的返回值 flags uint8 B uint8 // buckets的以2为底的对数, 即2^B=buckets noverflow uint16 // 溢出桶的近似数; see incrnoverflow for details hash0 uint32 // 哈希种子 buckets unsafe.Pointer // 2^B个bucket的数组,may be nil if count==0. oldbuckets unsafe.Pointer // 哈希在扩容时用于保存之前 buckets 的字段,它的大小是当前 buckets 的一半; nevacuate uintptr // progress counter for evacuation (buckets less than this have been evacuated) extra *mapextra // optional fields } 在上面我们需要关注的核心是buckets,它是一个指针,最终指向了bmap结构体数组 ...

2022-01-05 · 壹次心
« 上一页 
© 2025 握月担风 Powered by Hugo & PaperMod