2018-10-31 09:14:52 +08:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"gitee.com/johng/gf/g/util/gconv"
|
|
|
|
|
"gitee.com/johng/gf/g"
|
|
|
|
|
"fmt"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
type Score struct {
|
|
|
|
|
Name string
|
|
|
|
|
Result int
|
|
|
|
|
}
|
|
|
|
|
type User struct {
|
2019-01-14 13:55:07 +08:00
|
|
|
|
Scores []*Score
|
2018-10-31 09:14:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user := new(User)
|
|
|
|
|
scores := map[string]interface{}{
|
|
|
|
|
"Scores" : []interface{}{
|
|
|
|
|
map[string]interface{}{
|
|
|
|
|
"Name" : "john",
|
|
|
|
|
"Result" : 100,
|
|
|
|
|
},
|
|
|
|
|
map[string]interface{}{
|
|
|
|
|
"Name" : "smith",
|
|
|
|
|
"Result" : 60,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 嵌套struct转换,属性为slice类型,数值为slice map类型
|
|
|
|
|
if err := gconv.Struct(scores, user); err != nil {
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
} else {
|
|
|
|
|
g.Dump(user)
|
|
|
|
|
}
|
|
|
|
|
}
|