Golang Maps
package main
import (
"encoding/json"
"fmt"
"net/http"
)
type Address struct {
Street string `json:"street"`
Suite string `json:"suite"`
Zipcode string `json:"zipcode"`
}
type Users struct {
Name string `json:"name"`
Age uint `json:"age"`
}
func Mapping() {
// users 1 example
var users1 map[string]interface{}
users1 = map[string]interface{}{"name": "john doe", "age": 23}
// users 2 example
users2 := map[string]interface{}{"name": "john doe", "age": 23}
// users 3 example
users3 := make(map[string]interface{})
users3 = map[string]interface{}{"name": "John doe", "age": 23}
// users 4 example
var users4 map[string]interface{} = map[string]interface{}{"name": "John doe", "age": 23}
fmt.Printf("Object mapping 1 %#v \n", users1)
fmt.Printf("Object mapping 2 %#v \n", users2)
fmt.Printf("Object mapping 3 %#v \n", users3)
fmt.Printf("Object mapping 4 %#v \n", users4)
}
func ArrayMapping() {
// users 1 example
var users1 []map[string]interface{}
users1 = []map[string]interface{}{
map[string]interface{}{"name": "john doe", "age": 23},
}
// users 2 example
users2 := []map[string]interface{}{
map[string]interface{}{"name": "john doe", "age": 23},
}
// users 2 example
users3 := make([]map[string]interface{}, 1)
users3 = []map[string]interface{}{
map[string]interface{}{"name": "John doe", "age": 23},
}
// users 4 example
var users4 []map[string]interface{} = []map[string]interface{}{map[string]interface{}{"name": "John doe", "age": 23}}
fmt.Printf("Array object mapping 1 %#v \n", users1)
fmt.Printf("Array object mapping 2 %#v \n", users2)
fmt.Printf("Array object mapping 3 %#v \n", users3)
fmt.Printf("Array object mapping 4 %#v \n", users4)
}
func main() {
Mapping()
fmt.Printf("\n")
ArrayMapping()
}
Restu Wahyu Saputra