Shortcut to access interfaces.
go get -u github.com/michaellyu/entity
package main
import (
"github.com/michaellyu/entity"
"fmt"
)
func main() {
var data interface{} = map[string]interface{} {
"name": "Team",
"city": map[string]interface{} {
"name": "Tianjin",
},
"members": []interface{} {
map[string]interface{} {
"name": "Coral",
},
map[string]interface{} {
"name": "Jack",
},
map[string]interface{} {
"name": "Xinxing",
},
},
}
// new entity
team := entity.NewEntity(&data)
// get an interface{}
name, err := team.Get("name")
fmt.Println(name, err)
// get a string
cityName, _ := team.GetString("city.name")
fmt.Println(cityName)
// get a map
firstMember, _ := team.GetMap("members[0]")
fmt.Println(firstMember)
// get an array
members, _ := team.GetArray("members")
fmt.Println(members)
// set a field
err = team.Set("name", "One Team")
fmt.Println(err)
}
sudo docker-compose up -d
./test
./test -bench="."
sudo docker-compose down