Maps

Duration: 10 min•Difficulty: Medium•

A Map is a Key-Value data structure. In other languages, it's often called a Dictionary (Python), Hash (Ruby), or Object (JavaScript).

Concept

Imagine a dictionary. You look up a word ("Key") to find its meaning ("Value"). In a Map, keys must be unique.

When to use?

  • Storing student data by ID (ID -> Student Data).
  • Storing configurations (SettingName -> SettingValue).
  • Storing game scores (Username -> Score).
MAIN.GO
package main
import "fmt"
func main() {
// Creating a map: Key is string, Value is int
scores := make(map[string]int)
scores["Math"] = 90
scores["English"] = 85
fmt.Println("Scores:", scores)
// Accessing a specific value
fmt.Println("Math Score:", scores["Math"])
// Deleting data
delete(scores, "English")
fmt.Println("After delete:", scores)
}