How to Create and Use Maps in Go

Create Go maps with make or literals and access values using bracket notation with keys.

Create a map in Go using the make function or a literal, then access or modify values using bracket notation with keys. Use make for maps with a specific capacity or when you need to initialize it before use, and use literals for simple, immediate assignments.

// Create and use a map
scores := make(map[string]int)
scores["alice"] = 95
scores["bob"] = 88

// Access a value
fmt.Println(scores["alice"]) // Output: 95

// Check existence
if val, ok := scores["charlie"]; ok {
    fmt.Println(val)
} else {
    fmt.Println("Not found")
}

// Delete a key
delete(scores, "bob")