Files
gf/geg/util/gconv/gconv_struct6.go

39 lines
613 B
Go
Raw Normal View History

package main
import (
2019-04-03 00:03:46 +08:00
"fmt"
"github.com/gogf/gf/g"
"github.com/gogf/gf/g/util/gconv"
)
func main() {
2019-04-03 00:03:46 +08:00
type Score struct {
Name string
Result int
}
type User struct {
Scores []*Score
}
2019-04-03 00:03:46 +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,
},
},
}
2019-04-03 00:03:46 +08:00
// 嵌套struct转换属性为slice类型数值为slice map类型
if err := gconv.Struct(scores, user); err != nil {
fmt.Println(err)
} else {
g.Dump(user)
}
}