Make writing a habit together! This is the 8th day of my participation in the “Gold Digging Day New Plan · April More Text Challenge”. Click here for more details.
Scan conversion method can realize the conversion of any parameter to struct/struct array /map/map array, and automatically recognize the conversion according to the conversion target parameters entered by the developer.
Method definition
// Scan automatically calls MapToMap, MapToMaps, Struct or Structs function according to
// the type of parameter `pointer` to implement the converting.
// It calls function MapToMap if `pointer` is type of *map to do the converting.
// It calls function MapToMaps if `pointer` is type of *[]map/*[]*map to do the converting.
// It calls function Struct if `pointer` is type of *struct/**struct to do the converting.
// It calls function Structs if `pointer` is type of *[]struct/*[]*struct to do the converting.
func Scan(params interface{}, pointer interface{}, mapping ...map[string]string) (err error)
Copy the code
Automatic recognition conversionStruct
The structure of the body
The sample code
package main
import (
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/util/gconv"
)
func main(a) {
type User struct {
Uid int
Name string
}
params := g.Map{
"uid": 1."name": "Wang Zhongyang",}var user *User
iferr := gconv.Scan(params, &user); err ! =nil {
panic(err)
}
g.Dump(user)
}
Copy the code
The results
Automatic recognition conversionStruct
An array of
The sample code
package main
import (
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/util/gconv"
)
func main(a) {
type User struct {
Uid int
Name string
}
params := g.Slice{
g.Map{
"uid": 1."name": "Reflex",
},
g.Map{
"uid": 2."name": "Captain",}}var users []*User
iferr := gconv.Scan(params, &users); err ! =nil {
panic(err)
}
g.Dump(users)
}
Copy the code
The results
Automatically recognize transformation maps
The sample code
package main
import (
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/util/gconv"
)
func main(a) {
var (
user map[string]string
params = g.Map{
"uid": 1."name": "Wang Zhongyang",})iferr := gconv.Scan(params, &user); err ! =nil {
panic(err)
}
g.Dump(user)
}
Copy the code
The results
Automatic recognition conversionMap
An array of
package main
import (
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/util/gconv"
)
func main(a) {
var (
users []map[string]string
params = g.Slice{
g.Map{
"uid": 1."name": "Reflex",
},
g.Map{
"uid": 2."name": "Captain",}})iferr := gconv.Scan(params, &users); err ! =nil {
panic(err)
}
g.Dump(users)
}
Copy the code
The results
conclusion
In our development with Go, json data and structure conversion is often encountered.
GoFrame encapsulates the Scan conversion method for us, which can realize the conversion of any parameter to struct/struct array /map/map array, and automatically recognize and execute the conversion according to the conversion target parameter input by the developer.
The last
Thanks for reading and welcome to like, favorites,coin(attention)!!