go 语言中 map 的相关知识

简介: go 语言中 map 的相关知识

Go map key类型分析

1 map的key类型

map中的key可以是任何的类型,只要它的值能比较是否相等,Go的语言规范已精确定义,Key的类型可以是:

  • 布尔值
  • 数字
  • 字符串
  • 指针
  • 通道
  • 接口类型
  • 结构体
  • 只包含上述类型的数组

但不能是:

  • slice
  • map
  • function

Key类型只要能支持==!=操作符,即可以做为Key,当两个值==时,则认为是同一个Key。

2 比较相等

我们先看一下样例代码:

**package** main
**import** "fmt"
**type** _key **struct** {
}
**type** point **struct** {
  x **int**
  y **int**
}
**type** pair **struct** {
  x **int**
  y **int**
}
**type** Sumer **interface** {
  Sum() **int**
}
**type** Suber **interface** {
  Sub() **int**
}
**func** (p *pair) Sum() **int** {
  **return** p.x + p.y
}
**func** (p *point) Sum() **int** {
  **return** p.x + p.y
}
**func** (p pair) Sub() **int** {
  **return** p.x - p.y
}
**func** (p point) Sub() **int** {
  **return** p.x - p.y
}
**func** main() {
  fmt.Println("_key{} == _key{}: ", _key{} == _key{}) // output: true
  fmt.Println("point{} == point{}: ", point{x: 1, y: 2} == point{x: 1, y: 2})     // output: true
  fmt.Println("&point{} == &point{}: ", &point{x: 1, y: 2} == &point{x: 1, y: 2}) // output: false
  fmt.Println("[2]point{} == [2]point{}: ", 
    [2]point{point{x: 1, y: 2}, point{x: 2, y: 3}} == [2]point{point{x: 1, y: 2}, point{x: 2, y: 3}}) //output: true
  **var** a Sumer = &pair{x: 1, y: 2}
  **var** a1 Sumer = &pair{x: 1, y: 2}
  **var** b Sumer = &point{x: 1, y: 2}
  fmt.Println("Sumer.byptr == Sumer.byptr: ", a == b)        // output: false
  fmt.Println("Sumer.sametype == Sumer.sametype: ", a == a1) // output: false
  **var** c Suber = pair{x: 1, y: 2}
  **var** d Suber = point{x: 1, y: 2}
  **var** d1 point = point{x: 1, y: 2}
  fmt.Println("Suber.byvalue == Suber.byvalue: ", c == d)  // output: false
  fmt.Println("Suber.byvalue == point.byvalue: ", d == d1) // output: true
  ci1 := make(**chan** **int**, 1)
  ci2 := ci1
  ci3 := make(**chan** **int**, 1)
  fmt.Println("chan int == chan int: ", ci1 == ci2) // output: true
  fmt.Println("chan int == chan int: ", ci1 == ci3) // output: false
}

上面的例子让我们较直观地了解结构体,数组,指针,chan在什么场景下是相等。我们再来看Go语言规范中是怎么说的:

  • Pointer values are comparable. Two pointer values are equal if they point to the same variable or if both have value nil. Pointers to distinct zero-size variables may or may not be equal.当指针指向同一变量,或同为nil时指针相等,但指针指向不同的零值时可能不相等。
  • Channel values are comparable. Two channel values are equal if they were created by the same call to make or if both have value nil.Channel当指向同一个make创建的或同为nil时才相等
  • Interface values are comparable. Two interface values are equal if they have identical dynamic types and equal dynamic values or if both have value nil.从上面的例子我们可以看出,当接口有相同的动态类型并且有相同的动态值,或者值为都为nil时相等。要注意的是:参考理解Go Interface
  • A value x of non-interface type X and a value t of interface type T are comparable when values of type X are comparable and X implements T. They are equal if t’s dynamic type is identical to X and t’s dynamic value is equal to x.如果一个是非接口类型X的变量x,也实现了接口T,与另一个接口T的变量t,只t的动态类型也是类型X,并且他们的动态值相同,则他们相等。
  • Struct values are comparable if all their fields are comparable. Two struct values are equal if their corresponding non-blank fields are equal.结构体当所有字段的值相同,并且没有 相应的非空白字段时,则他们相等,
  • Array values are comparable if values of the array element type are comparable. Two array values are equal if their corresponding elements are equal.两个数组只要他们包括的元素,每个元素的值相同,则他们相等。

注意:Go语言里是无法重载操作符的,struct是递归操作每个成员变量,struct也可以称为map的key,但如果struct的成员变量里有不能进行==操作的,例如slice,那么就不能作为map的key。

3 类型判断

判断两个变量是否相等,首先是要判断变量的动态类型是否相同,在runtime中,_type结构是描述最为基础的类型(runtime/type.go),而map, slice, array等内置的复杂类型也都有对应的类型描述(如maptype,slicetype,arraytype)。

type _type struct {
  size       uintptr
  ptrdata    uintptr 
  hash       uint32
  tflag      tflag
  align      uint8
  fieldalign uint8
  kind       uint8
  alg        *typeAlg
  gcdata    *byte
  str       nameOff
  ptrToThis typeOff
}
...
type chantype struct {
  typ  _type
  elem *_type
  dir  uintptr
}
...
type slicetype struct {
  typ  _type
  elem *_type
}
...

其中对于类型的值是否相等,需要使用到成员alg *typeAlg(runtime/alg.go),它则持有此类型值的hash与equal的算法,它也是一个结构体:

type typeAlg struct {
  // function for hashing objects of this type
  // (ptr to object, seed) -> hash
  hash func(unsafe.Pointer, uintptr) uintptr
  // function for comparing objects of this type
  // (ptr to object A, ptr to object B) -> ==?
  equal func(unsafe.Pointer, unsafe.Pointer) bool
}

runtime/alg.go中提供了各种基础的hash funcequal func,例如:

func strhash(a unsafe.Pointer, h uintptr) uintptr {
  x := (*stringStruct)(a)
  return memhash(x.str, h, uintptr(x.len))
}
func strequal(p, q unsafe.Pointer) bool {
  return *(*string)(p) == *(*string)(q)
}

有了这些基础的hash funcequal func,再复杂的结构体也可以按字段递归计算hash与相等比较了。那我们再来看一下,当访问map[key]时,其实现对应在runtime/hashmap.go中的mapaccess1函数:

func mapaccess1(t *maptype, h *hmap, key unsafe.Pointer) unsafe.Pointer {
  ...
  alg := t.key.alg
  hash := alg.hash(key, uintptr(h.hash0)) // 1
  m := uintptr(1)<<h.B - 1
  b := (*bmap)(add(h.buckets, (hash&m)*uintptr(t.bucketsize))) // 2
  ...
  top := uint8(hash >> (sys.PtrSize*8 - 8))
  if top < minTopHash {
    top += minTopHash
  }
  for {
    for i := uintptr(0); i < bucketCnt; i++ {
      ...
      k := add(unsafe.Pointer(b), dataOffset+i*uintptr(t.keysize))
      if alg.equal(key, k) { // 3
        v := add(unsafe.Pointer(b), dataOffset+bucketCnt*uintptr(t.keysize)+i*uintptr(t.valuesize))
        ...
        return v
      }
    }
  ...
  }
}

mapaccess1的代码还是比较多的,简化逻辑如下(参考注释上序列):

  1. 调用key类型的hash方法,计算出keyhash
  2. 根据hash值找到对应的桶bucket
  3. 在桶中找到key值相等的map的value。判断相等需调用key类型的equal方法

到现在我们也就有了初步了解,map中的key访问时同时需要使用该类型的hash funcequal func,只要key值相等,当结构体即使不是同一对象,也可从map中获取相同的值,例如:

m := make(map[interface{}]interface{})
m[_key{}] = "value"
if v, ok := m[_key{}];ok {
  fmt.Println("%v", v) // output: value
}

判断两个 map 是否相等

比较两个map实例需要使用reflect包的DeepEqual()方法。如果相比较的两个map满足以下条件,方法返回true:


Map values are deeply equal when all of the following are true: they are both nil or both non-nil, they have the same length, and either they are the same map object or their corresponding keys (matched using Go equality) map to deeply equal values.


1.两个map都为nil或者都不为nil,并且长度要相等 they are both nil or both non-nil, they have the same length 2.相同的map对象或者所有key要对应相同 either they are the same map object or their corresponding keys 3.map对应的value也要深度相等 map to deeply equal values

Go 为什么不在语言层面支持 map 并发?

为什么原生不支持

凭什么 Go 官方还不支持,难不成太复杂了,性能太差了,到底是为什么?


官方答复原因如下(via @go faq):

  • 典型使用场景:map 的典型使用场景是不需要从多个 goroutine 中进行安全访问。

  • 非典型场景(需要原子操作):map 可能是一些更大的数据结构或已经同步的计算的一部分。

  • 性能场景考虑:若是只是为少数程序增加安全性,导致 map 所有的操作都要处理 mutex,将会降低大多数程序的性能。

核心来讲就是:Go 团队在经过了长时间的讨论后,认为原生 map 更应适配典型使用场景。


如果为了小部分情况,将会导致大部分程序付出性能代价,决定了不支持原生的并发 map 读写。且在 Go1.6 起,增加了检测机制,并发的话会导致异常。

为什么要崩溃

前面有提到一点,在 Go1.6 起会进行原生 map 的并发检测,这是一些人的 “噩梦”。


在此有人吐槽到:“明明给我抛个错就好了,凭什么要让我的 Go 进程直接崩溃掉,分分钟给我背个 P0”。

场景枚举

这里我们假设一下,如果并发读写 map 是以下两种场景:

  1. 产生 panic:程序 panic -> 默认走进 recover -> 没有对并发 map 进行处理 -> map 存在脏数据 -> 程序使用脏数据 -> 产生**未知((影响。
  2. 产生 crash:程序 crash -> 直接崩溃 -> 保全数据(数据正常)-> 产生**明确((风险。

你会选择哪一种方案呢?Go 官方在两者的风险衡量中选择了第二种。



无论是编程,还是人生。如何在随机性中掌握确定性的部分,也是一门极大的哲学了。

let it crash

Go 官方团队选择的方式是业内经典的 “let it crash” 行为,很多编程语言中,都会将其奉行为设计哲学。


let it crash 是指工程师不必过分担心未知的错误,而去进行面面俱到的防御性编码


这块理念最经典的就是 erlang 了。

总结

在今天这篇文章中,我们介绍了 Go 语言为什么不支持原生支持 map 并发,核心原因是大部分场景都不需要,从性能考虑上做的考虑。


直接让并发读写 map 的原因,是从 “let it crash” 去考虑。这块如果你想在自己的工程中避免这个情况,可以在 linter 等工具链加入竞态检测(-race),也可以避免这类风险。

相关文章
|
2月前
|
存储 JavaScript Java
(Python基础)新时代语言!一起学习Python吧!(四):dict字典和set类型;切片类型、列表生成式;map和reduce迭代器;filter过滤函数、sorted排序函数;lambda函数
dict字典 Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。 我们可以通过声明JS对象一样的方式声明dict
236 1
|
2月前
|
存储 安全 Java
【Golang】(4)Go里面的指针如何?函数与方法怎么不一样?带你了解Go不同于其他高级语言的语法
结构体可以存储一组不同类型的数据,是一种符合类型。Go抛弃了类与继承,同时也抛弃了构造方法,刻意弱化了面向对象的功能,Go并非是一个传统OOP的语言,但是Go依旧有着OOP的影子,通过结构体和方法也可以模拟出一个类。
232 1
|
10月前
|
编译器 Go
揭秘 Go 语言中空结构体的强大用法
Go 语言中的空结构体 `struct{}` 不包含任何字段,不占用内存空间。它在实际编程中有多种典型用法:1) 结合 map 实现集合(set)类型;2) 与 channel 搭配用于信号通知;3) 申请超大容量的 Slice 和 Array 以节省内存;4) 作为接口实现时明确表示不关注值。此外,需要注意的是,空结构体作为字段时可能会因内存对齐原因占用额外空间。建议将空结构体放在外层结构体的第一个字段以优化内存使用。
|
10月前
|
运维 监控 算法
监控局域网其他电脑:Go 语言迪杰斯特拉算法的高效应用
在信息化时代,监控局域网成为网络管理与安全防护的关键需求。本文探讨了迪杰斯特拉(Dijkstra)算法在监控局域网中的应用,通过计算最短路径优化数据传输和故障检测。文中提供了使用Go语言实现的代码例程,展示了如何高效地进行网络监控,确保局域网的稳定运行和数据安全。迪杰斯特拉算法能减少传输延迟和带宽消耗,及时发现并处理网络故障,适用于复杂网络环境下的管理和维护。
|
4月前
|
Cloud Native 安全 Java
Go:为云原生而生的高效语言
Go:为云原生而生的高效语言
317 1
|
5月前
|
存储 人工智能 安全
深入理解 go sync.Map - 基本原理
本文介绍了 Go 语言中 `map` 在并发使用时的常见问题及其解决方案,重点对比了 `sync.Mutex`、`sync.RWMutex` 和 `sync.Map` 的性能差异及适用场景。文章指出,普通 `map` 不支持并发读写,容易引发错误;而 `sync.Map` 通过原子操作和优化设计,在某些场景下能显著提升性能。同时详细讲解了 `sync.Map` 的基本用法及其适合的应用环境,如读多写少或不同 goroutine 操作不同键的场景。
255 1
|
4月前
|
Cloud Native Go API
Go:为云原生而生的高效语言
Go:为云原生而生的高效语言
410 0
|
4月前
|
Cloud Native Java Go
Go:为云原生而生的高效语言
Go:为云原生而生的高效语言
272 0
|
4月前
|
Cloud Native Java 中间件
Go:为云原生而生的高效语言
Go:为云原生而生的高效语言
252 0
|
4月前
|
Cloud Native Java Go
Go:为云原生而生的高效语言
Go:为云原生而生的高效语言
348 0