gf/geg/util/gconv/gconv_struct6.go

38 lines
787 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 {
Scores []*Score
}
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)
}
}