Golang数据结构之map

Golang数据结构之map

简介

用法

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//字面量初始化
hash1 := map[string]int{
    "1": 2,
    "3": 4,
    "5": 6,
}
//make初始化
hash2 := make(map[string]int, 3)
hash2["1"] = 2
hash2["3"] = 4
hash2["5"] = 6

//取值
_ = hash1[key]

//for 遍历
for k, v := range hash2 {
    // k, v
}

// delete
hash1[key] = value
hash1[key] = newValue
delete(hash, key)

底层实现

golang的map核心结构是hmap

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
type hmap struct {
    count     int
    flags     uint8
    B         uint8
    noverflow uint16
    hash0     uint32

    buckets    unsafe.Pointer
    oldbuckets unsafe.Pointer
    nevacuate  uintptr

    extra *mapextra
}

type mapextra struct {
    overflow    *[]*bmap
    oldoverflow *[]*bmap
    nextOverflow *bmap
}
type bmap struct {
    topbits  [8]uint8
    keys     [8]keytype
    values   [8]valuetype
    pad      uintptr
    overflow uintptr
}

参考

  1. https://segmentfault.com/a/1190000023879178

  2. 基本数据结构 - map的实现 - 《深入解析Go》 - 书栈网 · BookStack

  3. 理解 Golang 哈希表 Map 的原理 | Go 语言设计与实现

updatedupdated2024-08-252024-08-25