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

  

  

猜你喜欢

转载自www.cnblogs.com/LittleLee/p/9387915.html