for map golang code example
Example 1: create map golang
myMap := make(map[string]string)
myMap["key"] = "value"
value, ok := myMap["key"]
delete(myMap, "key")
Example 2: initialize map in golang
m := make(map[string]int)
m["Dio"] = 3
m["Jonathan"] = 1
Example 3: golang map
package main
import (
"fmt"
)
type User struct {
Name string
Age int
}
func main() {
var person = map[string]string{
"name": "john doe",
"age": "23",
}
var profile = make(map[string]string)
profile["name"] = "jane doe"
profile["age"] = "23"
var user = make(map[string]interface{})
user["name"] = "peter parker"
user["age"] = 30
var users = []map[string]interface{}{
{"name": "monkey d lufy", "age": 19},
{"name": "trafagar d law", "age": 23},
{"name": "nico robin", "age": 20},
}
var userStruct = map[string]User{
"name": {Name: "Monkey D Lufy"},
"age": {Age: 19},
}
fmt.Println(person)
fmt.Println(profile)
fmt.Println(user)
fmt.Println(users)
fmt.Println(userStruct)
}
Example 4: go get from map
var id string
var ok bool
if x, found := res["strID"]; found {
if id, ok = x.(string); !ok {
}
} else {
}