The map data type is available in many languages. It is a hash table in the form of key and value, so that key and value can be mapped one by one for fast search and addition. , delete and other operations. The Go language is no exception, providing the map data structure type.
# (Recommended learning: Go )
Golang, the map is a reference type, such as Like pointer slicing, it points to nil after being declared with the following code. This is also explained in the golang official documentation, so never use it directly after declaring it. You may often make the following mistakes at first:var m map[string]string m["result"] = "result"
m := make(map[string]string) m["result"] = "result"
map in golang is not concurrency safe
often Using map is usually very enjoyable, but suddenly one day the traffic increases and the program hangs up without knowing it. I don’t know what happened, but it was working fine before. Therefore, some good habits should be developed at the beginning, such as assertion checking, concurrency safety considerations, etc. Perhaps you can try sync.MapThe sync.Map in golang is concurrency-safe. In fact, it is a golang-customized structure named Map in the sync package. The structure prototype is as follows:type Map struct { mu Mutex read atomic.Value // readOnly dirty map[interface{}]*entry misses int }
var m sync.Map m.Store("method", "eth_getBlockByHash") value, ok := m.Load("method") t.Logf("value=%v,ok=%v\n",value,ok)
The above is the detailed content of What is the use of golang map?. For more information, please follow other related articles on the PHP Chinese website!