If the previous complex type conversion functionality is not sufficient, you can explore the Scan conversion method. This method can convert arbitrary parameters to struct/struct arrays/map/map arrays and automatically recognize and execute conversion based on the developer’s input target parameters.

The method is defined as follows:

  1. // Scan automatically calls MapToMap, MapToMaps, Struct or Structs function according to
  2. // the type of parameter `pointer` to implement the converting.
  3. // It calls function MapToMap if `pointer` is type of *map to do the converting.
  4. // It calls function MapToMaps if `pointer` is type of *[]map/*[]*map to do the converting.
  5. // It calls function Struct if `pointer` is type of *struct/**struct to do the converting.
  6. // It calls function Structs if `pointer` is type of *[]struct/*[]*struct to do the converting.
  7. func Scan(params interface{}, pointer interface{}, mapping ...map[string]string) (err error)

Let’s look at a few examples for a quick understanding.

Auto-recognition Conversion Struct

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. "github.com/gogf/gf/v2/util/gconv"
  5. )
  6. func main() {
  7. type User struct {
  8. Uid int
  9. Name string
  10. }
  11. params := g.Map{
  12. "uid": 1,
  13. "name": "john",
  14. }
  15. var user *User
  16. if err := gconv.Scan(params, &user); err != nil {
  17. panic(err)
  18. }
  19. g.Dump(user)
  20. }

After execution, the output result is:

  1. {
  2. Uid: 1,
  3. Name: "john",
  4. }

Auto-recognition Conversion Struct Array

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. "github.com/gogf/gf/v2/util/gconv"
  5. )
  6. func main() {
  7. type User struct {
  8. Uid int
  9. Name string
  10. }
  11. params := g.Slice{
  12. g.Map{
  13. "uid": 1,
  14. "name": "john",
  15. },
  16. g.Map{
  17. "uid": 2,
  18. "name": "smith",
  19. },
  20. }
  21. var users []*User
  22. if err := gconv.Scan(params, &users); err != nil {
  23. panic(err)
  24. }
  25. g.Dump(users)
  26. }

After execution, the terminal output is:

  1. [
  2. {
  3. Uid: 1,
  4. Name: "john",
  5. },
  6. {
  7. Uid: 2,
  8. Name: "smith",
  9. },
  10. ]

Auto-recognition Conversion to Map

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. "github.com/gogf/gf/v2/util/gconv"
  5. )
  6. func main() {
  7. var (
  8. user map[string]string
  9. params = g.Map{
  10. "uid": 1,
  11. "name": "john",
  12. }
  13. )
  14. if err := gconv.Scan(params, &user); err != nil {
  15. panic(err)
  16. }
  17. g.Dump(user)
  18. }

After execution, the output result is:

  1. {
  2. "uid": "1",
  3. "name": "john",
  4. }

Auto-recognition Conversion Map Array

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. "github.com/gogf/gf/v2/util/gconv"
  5. )
  6. func main() {
  7. var (
  8. users []map[string]string
  9. params = g.Slice{
  10. g.Map{
  11. "uid": 1,
  12. "name": "john",
  13. },
  14. g.Map{
  15. "uid": 2,
  16. "name": "smith",
  17. },
  18. }
  19. )
  20. if err := gconv.Scan(params, &users); err != nil {
  21. panic(err)
  22. }
  23. g.Dump(users)
  24. }

After execution, the output result is:

  1. [
  2. {
  3. "uid": "1",
  4. "name": "john",
  5. },
  6. {
  7. "uid": "2",
  8. "name": "smith",
  9. },
  10. ]