GoFrame框架Scan類型轉(zhuǎn)換實(shí)例
前言
Scan轉(zhuǎn)換方法可以實(shí)現(xiàn)對(duì)任意參數(shù)到struct/struct數(shù)組/map/map數(shù)組的轉(zhuǎn)換,并且根據(jù)開發(fā)者輸入的轉(zhuǎn)換目標(biāo)參數(shù)自動(dòng)識(shí)別執(zhí)行轉(zhuǎn)換。
方法定義
// 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)
自動(dòng)識(shí)別轉(zhuǎn)換Struct結(jié)構(gòu)體
示例代碼
package main
import (
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/util/gconv"
)
func main() {
type User struct {
Uid int
Name string
}
params := g.Map{
"uid": 1,
"name": "王中陽(yáng)",
}
var user *User
if err := gconv.Scan(params, &user); err != nil {
panic(err)
}
g.Dump(user)
}
運(yùn)行結(jié)果

自動(dòng)識(shí)別轉(zhuǎn)換Struct數(shù)組
示例代碼
package main
import (
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/util/gconv"
)
func main() {
type User struct {
Uid int
Name string
}
params := g.Slice{
g.Map{
"uid": 1,
"name": "優(yōu)弧",
},
g.Map{
"uid": 2,
"name": "船長(zhǎng)",
},
}
var users []*User
if err := gconv.Scan(params, &users); err != nil {
panic(err)
}
g.Dump(users)
}
運(yùn)行結(jié)果

自動(dòng)識(shí)別轉(zhuǎn)換Map
示例代碼
package main
import (
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/util/gconv"
)
func main() {
var (
user map[string]string
params = g.Map{
"uid": 1,
"name": "王中陽(yáng)",
}
)
if err := gconv.Scan(params, &user); err != nil {
panic(err)
}
g.Dump(user)
}
運(yùn)行結(jié)果

自動(dòng)識(shí)別轉(zhuǎn)換Map數(shù)組
示例代碼
package main
import (
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/util/gconv"
)
func main() {
var (
users []map[string]string
params = g.Slice{
g.Map{
"uid": 1,
"name": "優(yōu)弧",
},
g.Map{
"uid": 2,
"name": "船長(zhǎng)",
},
}
)
if err := gconv.Scan(params, &users); err != nil {
panic(err)
}
g.Dump(users)
}
運(yùn)行結(jié)果

總結(jié)
我們使用Go語(yǔ)言的開發(fā)過程中,json數(shù)據(jù)和結(jié)構(gòu)體的轉(zhuǎn)換是經(jīng)常遇到的。
GoFrame為我們封裝了Scan轉(zhuǎn)換方法可以實(shí)現(xiàn)對(duì)任意參數(shù)到struct/struct數(shù)組/map/map數(shù)組的轉(zhuǎn)換,并且根據(jù)開發(fā)者輸入的轉(zhuǎn)換目標(biāo)參數(shù)自動(dòng)識(shí)別執(zhí)行轉(zhuǎn)換。
以上就是GoFrame框架Scan類型轉(zhuǎn)換實(shí)例的詳細(xì)內(nèi)容,更多關(guān)于GoFrame框架Scan類型轉(zhuǎn)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- GoFrame框架使用避坑指南和實(shí)踐干貨
- GoFrame?gmap遍歷hashmap?listmap?treemap使用技巧
- GoFrame?gredis配置文件及配置方法對(duì)比
- 適合PHP同學(xué)的GoFrame框架使用體驗(yàn)及學(xué)習(xí)建議
- GoFrame?ORM原生方法操作示例
- GoFrame 框架緩存查詢結(jié)果的示例詳解
- GoFrame錯(cuò)誤處理常用方法及錯(cuò)誤碼使用示例
- GoFrame通用類型變量gvar與interface基本使用對(duì)比
- GoFrame框架數(shù)據(jù)校驗(yàn)之校驗(yàn)對(duì)象校驗(yàn)結(jié)構(gòu)體
- GoLang編程必備:GoFrame?GoLand插件介紹
相關(guān)文章
Go框架三件套Gorm?Kitex?Hertz基本用法與常見API講解
這篇文章主要為大家介紹了Go框架三件套Gorm?Kitex?Hertz的基本用法與常見API講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>2023-02-02
Go語(yǔ)言遍歷map實(shí)現(xiàn)(訪問map中的每一個(gè)鍵值對(duì))
這篇文章主要介紹了Go語(yǔ)言遍歷map實(shí)現(xiàn)(訪問map中的每一個(gè)鍵值對(duì)),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Go語(yǔ)言并發(fā)之context標(biāo)準(zhǔn)庫(kù)的使用詳解
Context的出現(xiàn)是為了解決在大型應(yīng)用程序中的并發(fā)環(huán)境下,協(xié)調(diào)和管理多個(gè)goroutine之間的通信、超時(shí)和取消操作的問題,本文就來(lái)和大家簡(jiǎn)單聊聊它的具體用法,希望對(duì)大家有所幫助2023-06-06
Go語(yǔ)言使用Timeout Context取消任務(wù)的實(shí)現(xiàn)
本文主要介紹了Go語(yǔ)言使用Timeout Context取消任務(wù)的實(shí)現(xiàn),包括基本的任務(wù)取消和控制HTTP客戶端請(qǐng)求的超時(shí),具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01
詳解go語(yǔ)言判斷管道是否關(guān)閉的常見誤區(qū)
這篇文章主要想和大家一起探討一下在Go語(yǔ)言中,我們是否可以使用讀取管道時(shí)的第二個(gè)返回值來(lái)判斷管道是否關(guān)閉,文中的示例代碼講解詳細(xì),有興趣的可以了解下2023-10-10

