Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Slices are good for storing collections of data, but the only way you can get data back out of a slice is by using a numeric index. Sometimes it's useful to be able to use other values as keys that let you look up items in a collection. Go uses the term "map" to refer to this sort of key-value list.
Sometimes it's useful to be able to use other values as keys that let you look up items in a collection. Go uses the term map to refer to this sort of key-value list.
The idea is that a map "maps" a set of keys to corresponding values. You can use any type you want as a key, as long as the keys are all the same type. If you know the key a value is stored under, you can use the key to retrieve that value.
package main
import "fmt"
func main() {
ages := map[string]float64{}
// Like the array/slice syntax, but you can use any value of the type you specified for the keys.
ages["Alice"] = 12
ages["Bob"] = 9
fmt.Println(ages)
}
- The syntax to retrieve an individual value is just like arrays or slices, too, except you use a key in place of a numeric index.
fmt.Println(ages["Alice"], ages["Bob"])
- Initialize map with values:
package main
import "fmt"
func main() {
ages := map[string]float64{"Alice": 12, "Bob": 9}
fmt.Println(ages)
}
- The built-in function
delete()
can be used to remove a key and its corresponding value:
delete(ages, "Bob")
- Maps also support
for ... range
loops:
package main
import "fmt"
func main() {
ages := map[string]float64{"Alice": 12, "Carol": 10, "Bob": 9}
for name, age := range ages {
fmt.Println(name, age)
}
}
- You can ignore the values from the map:
for name := range ages {
fmt.Println(name)
}
- Or ignore the keys:
for _, age := range ages {
fmt.Println(age)
}
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up