maps

Create

var states map[string]string
states := make(map[string]string)
commits := map[string]int{
    "rsc": 3711,
    "r":   2138,
    "gri": 1908,
    "adg": 912,
}

Get and set

states["CA"] = "California"
california := states["CA"]
cal, ok := s["CCCC"] //opt ok
  • if the key does not exist, then the zero of the value type is returned(0 for int, "" for string) and ok is false

Delete

delete(states, "CA") //[]

Iterate

for k, v := range states {
    fmt.Printf("%v, %v\n", k, v)
}

List

key := make([]string, len(states))
i := 0
for k := range states {
    keys[i] = k 
    i++
}

Last updated