gin使用自定義結(jié)構(gòu)綁定表單數(shù)據(jù)的示例代碼
以下示例使用自定義結(jié)構(gòu)
type StructA struct { FieldA string `form:"field_a"` } type StructB struct { NestedStruct StructA FieldB string `form:"field_b"` } type StructC struct { NestedStructPointer *StructA FieldC string `form:"field_c"` } type StructD struct { NestedAnonyStruct struct { FieldX string `form:"field_x"` } FieldD string `form:"field_d"` } func GetDataB(c *gin.Context) { var b StructB c.Bind(&b) c.JSON(200, gin.H{ "a": b.NestedStruct, "b": b.FieldB, }) } func GetDataC(c *gin.Context) { var b StructC c.Bind(&b) c.JSON(200, gin.H{ "a": b.NestedStructPointer, "c": b.FieldC, }) } func GetDataD(c *gin.Context) { var b StructD c.Bind(&b) c.JSON(200, gin.H{ "x": b.NestedAnonyStruct, "d": b.FieldD, }) } func main() { r := gin.Default() r.GET("/getb", GetDataB) r.GET("/getc", GetDataC) r.GET("/getd", GetDataD) r.Run() }
運(yùn)行示例:
$ curl "http://localhost:8080/getb?field_a=hello&field_b=world" {"a":{"FieldA":"hello"},"b":"world"} $ curl "http://localhost:8080/getc?field_a=hello&field_c=world" {"a":{"FieldA":"hello"},"c":"world"} $ curl "http://localhost:8080/getd?field_x=hello&field_d=world" {"d":"world","x":{"FieldX":"hello"}}
注意:不支持以下樣式結(jié)構(gòu)
type StructX struct { X struct {} `form:"name_x"` // HERE have form } type StructY struct { Y StructX `form:"name_y"` // HERE have form } type StructZ struct { Z *StructZ `form:"name_z"` // HERE have form }
總之,現(xiàn)在只支持現(xiàn)在沒(méi)有form
標(biāo)簽的自定義結(jié)構(gòu)
到此這篇關(guān)于gin使用自定義結(jié)構(gòu)綁定表單數(shù)據(jù)的示例代碼的文章就介紹到這了,更多相關(guān)gin綁定表單數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Go語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之選擇排序示例詳解
這篇文章主要為大家介紹了Go語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之選擇排序示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08Go并發(fā)編程結(jié)構(gòu)體多字段原子操作示例詳解
這篇文章主要為大家介紹了Go并發(fā)編程結(jié)構(gòu)體多字段原子操作示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12Go語(yǔ)言中如何確保Cookie數(shù)據(jù)的安全傳輸
這篇文章主要介紹了Go語(yǔ)言中如何確保Cookie數(shù)據(jù)的安全傳輸,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03詳解如何使用Golang實(shí)現(xiàn)Cron定時(shí)任務(wù)
定時(shí)任務(wù)是許多應(yīng)用程序中常見(jiàn)的一種需求,它們可以用于執(zhí)行定期的清理任務(wù),發(fā)送通知,生成報(bào)告等,在這篇博客中,我們將介紹如何在Go語(yǔ)言中使用robfig/cron包來(lái)實(shí)現(xiàn)Cron定時(shí)任務(wù),需要的朋友可以參考下2024-04-04Golang迭代如何在Go中循環(huán)數(shù)據(jù)結(jié)構(gòu)使用詳解
這篇文章主要為大家介紹了Golang迭代之如何在Go中循環(huán)數(shù)據(jù)結(jié)構(gòu)使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10Go語(yǔ)言并發(fā)之Select多路選擇操作符用法詳解
Go?語(yǔ)言借用多路復(fù)用的概念,提供了?select?關(guān)鍵字,用于多路監(jiān)聽(tīng)多個(gè)通道,本文就來(lái)和大家聊聊Go語(yǔ)言中Select多路選擇操作符的具體用法,希望對(duì)大家有所幫助2023-06-06