DEV Community

Toul
Toul

Posted on • Originally published at automatetheboringstuffwithgo.com

Ch 6. Maps

Like in real life, a map is a way of containing data that defines something. Think of the map of New York City. It is a piece of paper that includes data about the city's layout– depending on the map type.
Maps are typically handy when you combine different data types because a slice will only permit one data type per-use.
We'll briefly cover Maps so the reader can be aware of them and see when they might be useful.

Syntax

A map uses the 'map' keyword and [type] followed again by 'type.'

package main

func main() {
  nyc := map[string]int{
      "population": 8000000,
      "size": 302,
  }
  htx := map[string]int{
    "population": 2000000,
    "size": 665,
  }
}
Enter fullscreen mode Exit fullscreen mode

It says a map made of key-pair values of type string and int.
You can have any combination of types with maps, which is when you should consider using them over a slice.

Use case

Although the data is small above it is enough to let us access quick facts about each city with a fast lookup, with syntax similar to a slice.

package main

import "fmt"
func main() {
  nyc := map[string]int{
      "population": 8000000,
      "size": 302,
  }
  htx := map[string]int{
    "population": 2000000,
    "size": 665,
  }
  fmt.Println(htx["population"], nyc["population"])
}
Enter fullscreen mode Exit fullscreen mode

The curious reader should take a look at Go Maps in Action by the GO dev team to get a better sense of when to use 'maps'.

Conclusion

It is enough to know a map and how it generally works. Now, let's move on to part II of automating the boring stuff with go, which contains much more fun practical examples.

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

Top comments (0)