github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/blog/content/go-maps-in-action/people.go (about)

     1  // Copyright 2013 The Go Authors.  All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build OMIT
     6  
     7  package main
     8  
     9  import "fmt"
    10  
    11  func main() {
    12  	// START1 OMIT
    13  	type Person struct {
    14  		Name  string
    15  		Likes []string
    16  	}
    17  	var people []*Person
    18  
    19  	likes := make(map[string][]*Person) // HL
    20  	for _, p := range people {
    21  		for _, l := range p.Likes {
    22  			likes[l] = append(likes[l], p) // HL
    23  		}
    24  	}
    25  	// END1 OMIT
    26  
    27  	// START2 OMIT
    28  	for _, p := range likes["cheese"] {
    29  		fmt.Println(p.Name, "likes cheese.")
    30  	}
    31  	// END2 OMIT
    32  
    33  	fmt.Println(len(likes["bacon"]), "people like bacon.")
    34  }