mirror of
https://github.com/alrayyes/wiki.git
synced 2024-12-01 15:26:23 +00:00
125 lines
1.9 KiB
Markdown
125 lines
1.9 KiB
Markdown
---
|
|
id: 85fe90d9-f7d2-47fb-9471-f2557b1fa95d
|
|
title: Golang maps
|
|
---
|
|
|
|
# Basics
|
|
|
|
A map maps keys to values, ie: associative arrays. Zero value of a map
|
|
is `nil`. `make` function returns a new `map`.
|
|
|
|
``` go
|
|
package main
|
|
|
|
import "fmt"
|
|
|
|
type Vertex struct {
|
|
Lat, Long float64
|
|
}
|
|
|
|
var m map[string]Vertex
|
|
|
|
func main() {
|
|
m = make(map[string]Vertex)
|
|
m["Bell Labs"] = Vertex{
|
|
40.68433, -74.39967,
|
|
}
|
|
fmt.Println(m["Bell Labs"])
|
|
}
|
|
```
|
|
|
|
# Map literals
|
|
|
|
These are essentially the same as [struct
|
|
literals](20200828181259-structs), keys are required.
|
|
|
|
``` go
|
|
package main
|
|
|
|
import "fmt"
|
|
|
|
type Vertex struct {
|
|
Lat, Long float64
|
|
}
|
|
|
|
var m = map[string]Vertex{
|
|
"Bell Labs": Vertex{
|
|
40.68433, -74.39967,
|
|
},
|
|
"Google": Vertex{
|
|
37.42202, -122.08408,
|
|
},
|
|
}
|
|
|
|
func main() {
|
|
fmt.Println(m)
|
|
}
|
|
```
|
|
|
|
If top-level type is just a type name, it can be omitted from literal
|
|
elements. Saves some typing.
|
|
|
|
``` go
|
|
package main
|
|
|
|
import "fmt"
|
|
|
|
type Vertex struct {
|
|
Lat, Long float64
|
|
}
|
|
|
|
var m = map[string]Vertex{
|
|
"Bell Labs": {40.68433, -74.39967},
|
|
"Google": {37.42202, -122.08408},
|
|
}
|
|
|
|
func main() {
|
|
fmt.Println(m)
|
|
}
|
|
```
|
|
|
|
# Mutating maps
|
|
|
|
Nothing shocking here either. Map elements can be
|
|
added/deleted/modified/etc.
|
|
|
|
``` go
|
|
package main
|
|
|
|
import "fmt"
|
|
|
|
func main() {
|
|
m := make(map[string]int)
|
|
|
|
m["Answer"] = 42
|
|
fmt.Println("The value:", m["Answer"])
|
|
|
|
m["Answer"] = 48
|
|
fmt.Println("The value:", m["Answer"])
|
|
|
|
delete(m, "Answer")
|
|
fmt.Println("The value:", m["Answer"])
|
|
|
|
v, ok := m["Answer"]
|
|
fmt.Println("The value:", v, "Present?", ok)
|
|
}
|
|
```
|
|
|
|
# Types
|
|
|
|
A `type` can be declared as a thin wrapper around a `map`:
|
|
|
|
``` go
|
|
import "fmt"
|
|
|
|
type Dictionary map[string]string
|
|
|
|
func (d Dictionary) Search(word string) string {
|
|
return d[word]
|
|
}
|
|
|
|
func main() {
|
|
d := Dictionary{"key": "This is the value"}
|
|
fmt.Println(d.Search("key"))
|
|
}
|
|
```
|