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