mirror of
https://gitee.com/johng/gf.git
synced 2024-12-04 13:18:01 +08:00
33 lines
542 B
Go
33 lines
542 B
Go
package main
|
||
|
||
import (
|
||
"fmt"
|
||
|
||
"github.com/gogf/gf/frame/g"
|
||
"github.com/gogf/gf/util/gconv"
|
||
)
|
||
|
||
// 演示slice类型属性的赋值
|
||
func main() {
|
||
type User struct {
|
||
Scores []int
|
||
}
|
||
|
||
user := new(User)
|
||
scores := []interface{}{99, 100, 60, 140}
|
||
|
||
// 通过map映射转换
|
||
if err := gconv.Struct(g.Map{"Scores": scores}, user); err != nil {
|
||
fmt.Println(err)
|
||
} else {
|
||
g.Dump(user)
|
||
}
|
||
|
||
// 通过变量映射转换,直接slice赋值
|
||
if err := gconv.Struct(scores, user); err != nil {
|
||
fmt.Println(err)
|
||
} else {
|
||
g.Dump(user)
|
||
}
|
||
}
|