go language[5]-map

1. How the map is created

  • make函数   m := make(map[string]int)
  • The syntax for map literals: m := map[string]int{"a":1, "b":2}

2. Operation of map

  • Add/Modify m["a"] = 2
  • delete delete function delete(m, "a")
  • get a := m["a"]
    • If a lookup fails, it will return
      the zero value corresponding to the value type. For example, the following code will work even if "bob" does not exist in the map, because ages["bob"] will return
      0 on failure
    • The address operation cannot be performed on the map element
    • You can use the range function to traverse the map for name, age := range ages {}
    • The subscript syntax of map returns two values, the second is a bool, which is used to report whether the element exists, for example: if age, ok := m["age"]; !ok{....}
  • map can only be compared to nil
  • The key of map can be used to implement set

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325983096&siteId=291194637