Bitfield Consulting

View Original

Go maps: declaring and initializing

What is a Golang map? Why is it useful? How does it compare to a slice? How do you declare a map? How do you initialize a map in Go? Fear not, all these questions are answered in this friendly introduction to one of Go’s most powerful features.

Call 'em what you like—hashes, dictionaries, associative arrays, or, in the Go language, maps—this kind of data structure is familiar to every programmer:

{
    'eggs': 1.75,
    'bacon': 3.22,
    'sausage': 1.89,
}

What is a Golang map?

The map data type is built in to Go, so we use it a lot. Essentially, it lets us store some value (like the 1.75 value in the example above) and retrieve it by looking up a key (like eggs).

Why is this useful? Well, another way to store a bunch of related data values would be in a slice:

menu := []float64{1.75, 3.22, 1.89}

This is inconvenient to deal with, because we can only retrieve a value by its index (0, 1, 2, 3, etc), or by looking at every value in turn to see if it's the one we want. And we can't relate the values to other things. For example, which of these values corresponds to the price of eggs? It's impossible to know.

By contrast, a map lets us take one set of things (eggs, bacon, sausage), and set up a one-to-one correspondence (a mapping) with another set of things (1.75, 3.22, 1.89). Much more convenient when we want to look up the price of eggs, for example.

See this product in the original post

Learning Go is fun and easy, if you have the right book. For the Love of Go is a charming introduction to Go for beginners and experienced programmers alike.

Declaring and initializing maps

As usual in Go, we can declare our variable first, and then assign it a value:

var menu map[string]float64
menu = map[string]float64{
    "eggs": 1.75,
    "bacon": 3.22,
    "sausage": 1.89,
}

Alternatively, we can use the short declaration syntax to do both operations in one step:

menu := map[string]float64{
    "eggs": 1.75,
    "bacon": 3.22,
    "sausage": 1.89,
}

Golang map literals

The value assigned here is a map literal. It takes the form:

TYPE{
    KEY: VALUE,
    ...
}

Next

This is part 1 of a Golang tutorial series on maps. Check out the other posts in the series:

Now that you know how to declare and initialize a map, it's time to look at map types; what are the key and value types of a Go map, and how do we use them?

Find out more

You can learn more about maps in Go by following these links.

  • Go maps in action is a blog post from the Go team that gives a great overview of maps and their uses
  • The Tour of Go gives some nice interactive examples of Golang maps
  • Go by Example is a concise cheat sheet for map operations in Go
  • The Go language specification is the definitive reference for everything you need to know about maps

Read more

See this gallery in the original post

Looking for help with Go?

If you found this article useful, and you'd like to learn more about Go, then I can help! I offer one-to-one or group training in Go development, for complete beginners through to experienced Gophers. Check out my Learn Golang with John page to find out more, or email go@bitfieldconsulting.com. Looking forward to hearing from you!

See this content in the original post