github.com/zhongdalu/gf@v1.0.0/geg/util/gconv/gconv_struct6.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "github.com/zhongdalu/gf/g" 6 "github.com/zhongdalu/gf/g/util/gconv" 7 ) 8 9 func main() { 10 type Score struct { 11 Name string 12 Result int 13 } 14 type User struct { 15 Scores []*Score 16 } 17 18 user := new(User) 19 scores := map[string]interface{}{ 20 "Scores": []interface{}{ 21 map[string]interface{}{ 22 "Name": "john", 23 "Result": 100, 24 }, 25 map[string]interface{}{ 26 "Name": "smith", 27 "Result": 60, 28 }, 29 }, 30 } 31 32 // 嵌套struct转换,属性为slice类型,数值为slice map类型 33 if err := gconv.Struct(scores, user); err != nil { 34 fmt.Println(err) 35 } else { 36 g.Dump(user) 37 } 38 }