duanhan3067 2018-06-27 18:55
浏览 70
已采纳

嵌套地图的情况下,map.LoadOrStore返回的地址值是否与输入相同?

I'm doing a nested sync.Map but I wonder if I can save some few lines of code if the value returned by LoadOrStore is the same as the input in case of a map, I mean this:

var mapa sync.Map
mapaInterFace, ok := sessiones.LoadOrStore(userID,mapa)
if ok {
    mapa,ok=mapaInterFace.(sync.Map)
    if !ok{
        return errors.New("type assertion")
    }
}

If mapa were the same as the returned by LoadOrStore when the value is stored, I can immediately use it, but if not I have to add after the previous code, the type assertion:

mapa,ok=mapaInterFace.(sync.Map)
    if !ok{
        return errors.New("type assertion")
    }

and doing some often it can make some ugly code

Update: sessiones is type sync.Map

  • 写回答

1条回答 默认 最新

  • duanjuelian4640 2018-06-27 20:09
    关注

    As I explain later, you should use pointers for the sync.Map types. Therefore, we can simplify to:

    var mapa, mapb = new(sync.Map), new(sync.Map)
    var key string
    
    if actual, loaded := mapa.LoadOrStore(key, mapb); loaded {
        if maps, ok := actual.(*sync.Map); ok {
            mapb = maps
        } else {
            // handle loaded value type assertion error
        }
    }
    

    Now the assignments are cheap because we are assigning pointers (*sync.Map) not structs (sync.Map).


    Package sync

    import "sync" 
    

    type Map

    Map is like a Go map[interface{}]interface{} but is safe for concurrent use by multiple goroutines without additional locking or coordination. Loads, stores, and deletes run in amortized constant time.

    The Map type is specialized. Most code should use a plain Go map instead, with separate locking or coordination, for better type safety and to make it easier to maintain other invariants along with the map content.

    The Map type is optimized for two common use cases: (1) when the entry for a given key is only ever written once but read many times, as in caches that only grow, or (2) when multiple goroutines read, write, and overwrite entries for disjoint sets of keys. In these two cases, use of a Map may significantly reduce lock contention compared to a Go map paired with a separate Mutex or RWMutex.

    The zero Map is empty and ready for use. A Map must not be copied after first use.

    type Map struct {
            // contains filtered or unexported fields
    }
    

    func (*Map) LoadOrStore

    func (m *Map) LoadOrStore(key, value interface{}) (actual interface{}, loaded bool)
    

    LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.


    A sync.Map must not be copied after first use.

    In Go, all arguments and receivers are passed by value, as if by assignment (a copy). For example, go vet reports a sync.Map copy error,

    // go vet: variable declaration copies lock value to arg: sync.Map contains sync.Mutex
    var m sync.Map
    var arg interface{} = m
    

    and

    var map1, map2 sync.Map
    // go vet: call of map1.LoadOrStore copies lock value: sync.Map contains sync.Mutex
    map1.LoadOrStore("key", map2)
    

    Use pointers. For example,

        var m sync.Map
        var arg interface{} = &m
    

    and

        var map1, map2 sync.Map
        map1.LoadOrStore("key", &map2)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图