Categories: GoFrame 教程

GoFrame 类型转换-Scan转换

前面关于复杂类型的转换功能如果大家觉得还不够的话,那么您可以了解下​Scan​转换方法,该方法可以实现对任意参数到​struct/struct​数组​/map/map​数组的转换,并且根据开发者输入的转换目标参数自动识别执行转换。

该方法定义如下:

// 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)

我们接下来看几个示例便可快速理解。

自动识别转换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数组

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.Slice{
  g.Map{
   "uid":  1,
   "name": "john",
  },
  g.Map{
   "uid":  2,
   "name": "smith",
  },
 }
 var users []*User
 if err := gconv.Scan(params, &users); err != nil {
  panic(err)
 }
 g.Dump(users)
}

执行后,终端输出:

[
    {
            "Uid": 1,
            "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)
}

执行后,输出结果为:

{
   "Uid": "1", 
   "Name": "john"
}

自动识别转换Map数组

package main

import (
 "github.com/gogf/gf/v2/frame/g"
 "github.com/gogf/gf/v2/util/gconv"
)

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"
    }
]

冒牌SEO

前端开发者,欢迎大家一起沟通和交流。

Share
Published by
冒牌SEO

Recent Posts

聊聊vue3中的defineProps

在Vue 3中,defineP…

1 周 ago

在 Chrome 中删除、允许和管理 Cookie

您可以选择删除现有 Cooki…

2 周 ago

自定义指令:聊聊vue中的自定义指令应用法则

今天我们来聊聊vue中的自定义…

3 周 ago

聊聊Vue中@click.stop和@click.prevent

一起来学下聊聊Vue中@cli…

4 周 ago

Nginx 基本操作:启动、停止、重启命令。

我们来学习Nginx基础操作:…

1 月 ago

Vue3:手动清理keep-alive组件缓存的方法

Vue3中手动清理keep-a…

1 月 ago