该方法定义如下:
自动识别转换Struct
package main
import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
)
func main() {
type User struct {
Uid int
Name string
}
params := g.Map{
"uid": 1,
"name": "john",
}
var user *User
if err := gconv.Scan(params, &user); err != nil {
panic(err)
}
g.Dump(user)
}
执行后,输出结果为:
{
"Name": "john",
"Uid": 1
}
自动识别转换Struct
数组
{
"Name": "john"
},
{
"Uid": 2,
"Name": "smith"
}
]
自动识别转换Map
package main
import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
)
func main() {
var (
user map[string]string
params = g.Map{
"uid": 1,
"name": "john",
}
)
if err := gconv.Scan(params, &user); err != nil {
panic(err)
}
g.Dump(user)
}
执行后,输出结果为:
自动识别转换Map
数组
package main
"github.com/gogf/gf/v2/frame/g"
)
func main() {
var (
users []map[string]string
params = g.Slice{
g.Map{
"uid": 1,
"name": "john",
},
g.Map{
"uid": 2,
"name": "smith",
},
}
)
if err := gconv.Scan(params, &users); err != nil {
panic(err)
}
g.Dump(users)
}
[
{
"name": "john",
"uid": "1"
},
{
"name": "smith",
"uid": "2"
}