4. Maps and Control Structures

This chapter covers the basics of maps and control structures. We explain how maps are a powerful tool for storing key-value pairs. This chapter also covers control structures such as if and switch statements.

Maps

Maps are a powerful built-in data structure that associates keys and values. A map is an unordered set of values indexed by a unique key. Map keys and values can be set and retrieved using the [] syntax. In Listing 4.1, we create a new map, map[string]string{}, and assign it to the users variable. To assign a value, we can access the key using the users[<key>] syntax. In this case, we are accessing the key, Janis, for assignment. To retrieve the Janis key from the map, we can reverse the process. If ...

Get Go Fundamentals: Gopher Guides now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.