make is a Go built-in function that allocates and initializes maps, slices, or channels. For maps, an optional size hint makes expected capacity explicit; omitting it or passing zero has the same behavior. section 3.2.1
Use make according to the collection being initialized:
- Initialize a map with an optional expected size: count := make(map[Book]uint, 451). section 3.2.1
- Initialize a slice with a length: books := make([]Book, 5) creates five elements that can be indexed immediately. section 3.2.2
- Initialize a slice with capacity but no elements: books := make([]Book, 0, 5), then grow it with append. section 3.2.2
Counting books with a map
func booksCount(bookworms []Bookworm) map[Book]uint { count := make(map[Book]uint) for _, bookworm := range bookworms { for _, book := range bookworm.Books { count[book]++ } } return count } The map stores each Book as a key and its occurrence count as a uint value. A known number of distinct books can be supplied as a size hint.
Collecting repeated books
var commonBooks []Book for book, count := range booksOnShelves { if count > 1 { commonBooks = append(commonBooks, book) } } A nil slice can grow with append, while make([]Book, 0, 5) reserves capacity without adding elements.

