golang数据结构之sync.Map篇


package main

import (
   "fmt"
   "sync"
)

func main() {
   var mapInt = new(sync.Map)
   //add elem
   mapInt.Store(1, 1)
   mapInt.Store(2, 2)
   mapInt.Store(3, 3)
   fmt.Println("before delete key:")
   // iterator
   mapInt.Range(func(key, value interface{}) bool {
      fmt.Println(key, value)
      return true
   })
   // del
   mapInt.Delete(2)
   fmt.Println("after delete key:")
   mapInt.Range(func(key, value interface{}) bool {
      fmt.Println(key, value)
      return true
   })
   // query
   v, ok := mapInt.Load(1)
   if ok {
      fmt.Println(v)
   }
   // load or store
   v, ok = mapInt.LoadOrStore(2, 10)
   fmt.Println("load or store:", v, ", ok:", ok)

}
output: 
before delete key:
1 1
2 2
3 3
after delete key:
1 1
3 3
1
load or store: 10 , ok: false

  

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM