Search⌘ K

Maps in Go

Explore the concept of maps in Go, how they function as key-value collections similar to dictionaries, and learn to create, update, retrieve, and delete elements. Understand how to check for key presence and handle map operations effectively for better program design.

We'll cover the following...

Introduction

Maps are somewhat similar to what other languages call “dictionaries” or “hashes”.

A map maps​ keys to values. Here we are mapping string keys (actor names) to an integer value (age).

Go (1.6.2)
package main
import "fmt"
func main() {
celebs := map[string]int{ //mapping strings to integers
"Nicolas Cage": 50,
"Selena Gomez": 21,
"Jude Law": 41,
"Scarlett Johansson": 29,
}
fmt.Printf("%#v", celebs)
}

When not using map literals like above, maps must be created with make (not new) before use. The nil map is ...