github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/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  package main
     6  
     7  import "fmt"
     8  
     9  func main() {
    10  	// START1 OMIT
    11  	type Person struct {
    12  		Name  string
    13  		Likes []string
    14  	}
    15  	var people []*Person
    16  
    17  	likes := make(map[string][]*Person) // HL
    18  	for _, p := range people {
    19  		for _, l := range p.Likes {
    20  			likes[l] = append(likes[l], p) // HL
    21  		}
    22  	}
    23  	// END1 OMIT
    24  
    25  	// START2 OMIT
    26  	for _, p := range likes["cheese"] {
    27  		fmt.Println(p.Name, "likes cheese.")
    28  	}
    29  	// END2 OMIT
    30  
    31  	fmt.Println(len(likes["bacon"]), "people like bacon.")
    32  }