...

/

Maps in Go

Maps in Go

This lesson gives an introduction to maps, using the map literals and mutating maps in Go

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).

Press + to interact
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) ...

Access this course and 1400+ top-rated courses and projects.