golang sync.map 包

Detailed sync.Map

sync.Map 1.9 is only recommended concurrent security map.

main Package 

Import ( 
	"FMT" 
	"Sync" 
) 

FUNC main () { 
	var SM sync.Map 
	// Store function additive elements 
	sm.Store (. 1, "A") 
	// get the Load function value 
	IF V, OK: SM = .load (. 1); OK { 
		fmt.Println (V) 
	} 
	/ * 
		LoadOrStore Get function or add. 
		Parameter is the key, value. 
		If this key exists and is not marked deleted the original value is returned (not updated) and true. 
		It does not exist, store, and returns the value false. 
	* / 
	IF V, OK: sm.LoadOrStore = (. 1, "B"); OK { 
		fmt.Println (V) 
	} 
	IF V, OK:! = Sm.LoadOrStore (2, "C"); OK { 
		FMT. println (V) 
	} 
	// remove 
	sm.Delete (. 1) 
	// traverse, as a function of the parameters. Bool return value of a function parameter key value, return false traverse swing stop
	sm.Range (FUNC (K, V interface {}) {BOOL 
		fmt.Printf ( "Key =% D, S value =% \ n-", K, V) 
		return to true 
	}) 
	//sync.map not supported len parameters, needs to traverse the length of the acquisition, more complex. 
	Fmt.Println // (len (SM)) 
}

  

Guess you like

Origin www.cnblogs.com/lemonzwt/p/11415553.html