diff options
-rw-r--r-- | content/go-maps-in-action.article | 35 | ||||
-rw-r--r-- | content/go-maps-in-action/list.go | 27 | ||||
-rw-r--r-- | content/go-maps-in-action/people.go | 32 |
3 files changed, 63 insertions, 31 deletions
diff --git a/content/go-maps-in-action.article b/content/go-maps-in-action.article index 0066ed6..f6d59a7 100644 --- a/content/go-maps-in-action.article +++ b/content/go-maps-in-action.article @@ -85,48 +85,21 @@ It can be convenient that a map retrieval yields a zero value when the key is no For instance, a map of boolean values can be used as a set-like data structure (recall that the zero value for the boolean type is false). This example traverses a linked list of `Nodes` and prints their values. It uses a map of `Node` pointers to detect cycles in the list. - type Node struct { - Next *Node - Value interface{} - } - var first *Node - - *visited*:=*make(map[*Node]bool)* - for n := first; n != nil; n = n.Next { - if *visited[n]* { - fmt.Println("cycle detected") - break - } - *visited[n]*=*true* - fmt.Println(n.Value) - } +.code go-maps-in-action/list.go /START/,/END/ The expression `visited[n]` is `true` if `n` has been visited, or `false` if `n` is not present. There's no need to use the two-value form to test for the presence of `n` in the map; the zero value default does it for us. Another instance of helpful zero values is a map of slices. Appending to a nil slice just allocates a new slice, so it's a one-liner to append a value to a map of slices; there's no need to check if the key exists. In the following example, the slice people is populated with `Person` values. Each `Person` has a `Name` and a slice of Likes. The example creates a map to associate each like with a slice of people that like it. - type Person struct { - Name string - Likes []string - } - var people []*Person - - *likes*:=*make(map[string][]*Person)* - for _, p := range people { - for _, l := range p.Likes { - *likes[l]*=*append(likes[l],*p)* - } - } +.code go-maps-in-action/people.go /START1/,/END1/ To print a list of people who like cheese: - for _, p := range likes["cheese"] { - fmt.Println(p.Name, "likes cheese.") - } +.code go-maps-in-action/people.go /START2/,/END2/ To print the number of people who like bacon: - fmt.Println("People that like bacon:", len(likes["bacon"])) +.code go-maps-in-action/people.go /bacon/ Note that since both range and len treat a nil slice as a zero-length slice, these last two examples will work even if nobody likes cheese or bacon (however unlikely that may be). diff --git a/content/go-maps-in-action/list.go b/content/go-maps-in-action/list.go new file mode 100644 index 0000000..09feeae --- /dev/null +++ b/content/go-maps-in-action/list.go @@ -0,0 +1,27 @@ +// Copyright 2013 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +import "fmt" + +func main() { + // START OMIT + type Node struct { + Next *Node + Value interface{} + } + var first *Node + + visited := make(map[*Node]bool) // HL + for n := first; n != nil; n = n.Next { + if visited[n] { // HL + fmt.Println("cycle detected") + break + } + visited[n] = true // HL + fmt.Println(n.Value) + } + // END OMIT +} diff --git a/content/go-maps-in-action/people.go b/content/go-maps-in-action/people.go new file mode 100644 index 0000000..e724915 --- /dev/null +++ b/content/go-maps-in-action/people.go @@ -0,0 +1,32 @@ +// Copyright 2013 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +import "fmt" + +func main() { + // START1 OMIT + type Person struct { + Name string + Likes []string + } + var people []*Person + + likes := make(map[string][]*Person) // HL + for _, p := range people { + for _, l := range p.Likes { + likes[l] = append(likes[l], p) // HL + } + } + // END1 OMIT + + // START2 OMIT + for _, p := range likes["cheese"] { + fmt.Println(p.Name, "likes cheese.") + } + // END2 OMIT + + fmt.Println(len(likes["bacon"]), "people like bacon.") +} |