This is a creation in Article, where the information may have evolved or changed.
Associative array: (hash or dictionary)
Map is the go built-in correlation data type, the dictionary is accessed by Key to Value , the access format is as follows:
Value=mapname[key]
In fact, the array can be regarded as a dictionary of key-value type integer, which can be said that the array is a special dictionary.
1. Dictionary Item Lookup
To find a specific key-value pair from the dictionary, you can:
V,ok:=mapname[key]
After executing this statement, if the key value of the lookup exists, assign the value of the key corresponding to v, OK to be true , conversely, v equals 0 , OK bit false . Test Case:
var map1 = map[string]int{"Key1": +, "Key2": 200}
v,ok:=map1["Key1"]
If ok{
Fmt. Println (V,ok)
}else{
Fmt,println (v)
}
2. Deletion and addition of dictionaries
The code example is shown below:
var map1 = map[string]int{"Key1": 1, "Key2": 2, "Key3": 3}
For k, V: = Range Map1 {
Fmt. Println (k, v)
if k = = "Key1" {
Delete (Map1, k)
}
if k = = "Key3" {
map1["Key2"] = 5
}
}
Fmt. Println (MAP1)
Operation Result:
Key1 1
Key2 2
Key3 3
Map[key3:3 Key2:5]
The dictionary is relatively simple, do not spend too much ink to repeat the