Go language Learning Note 13: Map Collection
Map is basically in each language, Java is a collection class map, which includes HashMap, TreeMap and so on. The Python language is directly a type that is simpler than Java.
Map in the go language is simpler than Java, and more cumbersome than python.
Define Map
var x map[string]stringx : = make(map[string]string)
There are some strange ways to do this, the map is the keyword, the right-hand bracket is the type of key, and the outer bracket is the type of value. In general, a comma or colon is used to separate key and value, but the go language does not follow this principle, but instead uses parentheses and parentheses.
And the map must be initialized, otherwise it will become nil map, and nil map cannot be used to store key-value pairs.
package mainimport "fmt"func main() { var x map[string]string x = make(map[string]string) x["a"] = "1"; x["b"] = "2"; for item := range x { fmt.Println(item, x[item]); } value, exist := x["a"] if (exist) { fmt.Println("x has", value); }}
Delete function
The delete () function is used to delete the elements of the collection, with the parameter map and its corresponding key.
fruits := map[string]string {"apple": "12", "orange": "210"}delete(fruits, "apple")
Go language Learning Note 13: Map Collection