Maps with slices as values

suggest change
	m := make(map[string][]int)
	fmt.Printf("A missing key returns the zero value of []int, nil: %#v\n", m["key1"])

	m["key1"] = append(m["key1"], 8)
	m["key1"] = append(m["key1"], 3)
	fmt.Printf("\nAfter appending to slice: %#v\n", m["key1"])

	delete(m, "key1")
	fmt.Printf("\nAfter delete, lookup returns a nil slice: %#v\n", m["key1"])
A missing key returns the zero value of []int, nil: []int(nil)

After appending to slice: []int{8, 3}

After delete, lookup returns a nil slice: []int(nil)

In m["key1"] = append(m["key1"], 8), there is no need to check whether m["key1"] exists. Three rules make this work:

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents