golang dictionary code example
Example 1: initialize map in golang
m := make(map[string]int)
m["Dio"] = 3
m["Jonathan"] = 1
Example 2: dictionary in golang
m := make(map[string]float64)
m["pi"] = 3.14
m["pi"] = 3.1416
fmt.Println(m)
v := m["pi"]
v = m["pie"]
_, found := m["pi"]
_, found = m["pie"]
if x, found := m["pi"]; found {
fmt.Println(x)
}
delete(m, "pi")
fmt.Println(m)
Example 3: go add to map
m := make(map[string]int)
m["numberOne"] = 1
m["numberTwo"] = 2
Example 4: dictionary golang
package main
import (
"fmt"
)
func main() {
dict := map[interface{}]interface{} {
1: "hello",
"hey": 2,
}
fmt.Println(dict)
}
Example 5: go get from map
var id string
var ok bool
if x, found := res["strID"]; found {
if id, ok = x.(string); !ok {
}
} else {
}