Maps 1
package main import "fmt" /* * Go maps are just what they sound like. */ func main() { // A Go map needs to be explicitly allocated. Specify key and // content types. m := make(map[string]int) m["foo"] = 1 m["bar"] = 20 m["nimrod"] = 5 m["smith"] = 18 m["thud"] = 7 fmt.Printf("%s => %d, %s => %d, %s => %d\n", "bar", m["bar"], "smith", m["smith"], "jones", m["jones"]) fmt.Println() for key, elt := range m { fmt.Printf("%s => %d\n", key, elt) } fmt.Println() delete(m,"nimrod") for key, elt := range m { fmt.Printf("%s => %d\n", key, elt) } // There is a sort of map literal. Note the comma at the end. m = map[string]int { "bill" : 18, "alice" : 20, "boris" : 3, "fannie" : 18, "mike" : 34, "tim" : 3, } fmt.Println() for key, elt := range m { fmt.Printf("%s => %d\n", key, elt) } }