Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
package main
import (
"fmt"
)
type (
user struct {
ID int `json:"id"`
Name string `json:"name"`
}
)
var (
users = map[int]*user{}
)
func main() {
var map1 map[string]string = map[string]string{}
fmt.Println(map1)
//map[]
var map2 = make(map[string]string)
fmt.Println(map2)
//map[]
map3 := map[string]string{}
fmt.Println(map3)
//map[]
var map4 map[string]string = map[string]string{"one": "apple", "two": "orange"}
fmt.Println(map4)
//map[two:orange one:apple]
map5 := map[string]string{"one": "apple", "two": "orange"}
fmt.Println(map5)
//map[two:orange one:apple]
map6 := map[string]string{
"one": "apple",
"two": "orange",
}
fmt.Println(map6)
//map[two:orange one:apple]
map7 := make(map[string]string)
map7["one"] = "apple"
map7["two"] = "orange"
fmt.Println(map7)
//map[two:orange one:apple]
map8 := map[int]string{
1: "apple",
2: "orange",
}
fmt.Println(map8)
//map[1:apple 2:orange]
if val, ok := map8[1]; ok {
fmt.Printf("exists %#v", val)
} else {
fmt.Printf("not exists")
}
//exists "apple"
u := &user{
ID: 1,
Name: "mike",
}
users[u.ID] = u
fmt.Println(users[1])
//&{1 mike}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment