golang在多个go routine中进行map或者slice操作应该注意的对象。

因为golang的map和列表切片都是引用类型,且非线程安全的,所以在多个go routine中进行读写操作的时候,会产生“map read and map write“的panic错误。

某一些类型的对象,会有这种类似的set方法来写数据,或者get方法来返回一个map:

func (this *object) Set(name, val) {
  this.Lock()
  defer this.Unlock()
  
  this.m[name] = val
}

func (this *object) Get() map[string]string {
  this.Lock()
  defer this.Unlock()

  return this.m
}

  

如果会在多个go routine中通过该对象的Get()方法获取到的map进行读操作,并且在其他go routine中用Set()方法来写操作,那么有可能会导致“map read and map write“的panic错误。

原因是Get方法获取的map和Set方法操作的map是同一个map,如果读写线程在同一时刻操作这2个map,就会产生错误。

所以Get方法最好用这种方式返回map:

func (this *object) Get() map[string]string {
    this.Lock()
    defer this.Unlock()

    newm := make(map[string]string)
    for k, v := range this.m {
        newm[k] = v
    }

    return newm
}

这样每次Get获取的map,其实是一个新的map,就可以不用考虑同时读写的问题了。

原文地址:https://www.cnblogs.com/yzhch/p/8137128.html