forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example1.go
42 lines (33 loc) · 1012 Bytes
/
example1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// http://play.golang.org/p/WZ7qt_I8GY
// Sample program to show how to declare, initialize and iterate
// over a map. Shows how iterating over a map is random.
package main
import "fmt"
// user defines a user in the program.
type user struct {
name string
email string
}
// main is the entry point for the application.
func main() {
// Declare and make a map that stores values
// of type user with a key of type string.
users := make(map[string]user)
// Add key/value pairs to the map.
users["Roy"] = user{"Rob", "Roy"}
users["Ford"] = user{"Henry", "Ford"}
users["Mouse"] = user{"Mickey", "Mouse"}
users["Jackson"] = user{"Michael", "Jackson"}
// Iterate over the map.
for key, value := range users {
fmt.Println(key, value)
}
fmt.Println()
// Iterate over the map and notice the
// results are different.
for key := range users {
fmt.Println(key)
}
}