capypad
0 day streak
go / beginner
Snippet

Working with Maps

Maps are Go's built-in associative data type (hash tables). they store data in key-value pairs.

snippet.go
go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package main
 
import "fmt"
 
func main() {
menu := map[string]float64{
"Coffee": 2.50,
"Tea": 2.00,
}
 
menu["Juice"] = 3.25
delete(menu, "Tea")
 
fmt.Println(menu)
}
Breakdown
1
map[string]float64
Defines a map where keys are strings and values are decimal numbers.
2
menu["Juice"] = 3.25
Adds a new key-value pair or updates an existing one.
3
delete(menu, "Tea")
A built-in function to remove a specific key from the map.