-1
type _getData struct {
Title string `json:"title" form:"title"`
Date  string `json:"date" form:"date"`
Pages []struct {
    Order       int    `json:"order" form:"title"`
    Description string `json:"description" form:"description"`
} `json:"pages" form:"pages"`


func CreateDiary(c echo.Context) error {

var getData _getData
c.Bind(&getData)
fmt.Print(getData)
...

通过 c.FormParams 命令接收以下数据,请告诉我如何将其绑定到_getDatastruct,

map[address:[미국 캘리포니아 산타클라라 카운티 쿠퍼티노 ] date:[2021-10-05] location:[37.32779072192643 -122.01981157064436] map_id:[0] pages[0][description]:[123123] pages[0][order]:[0] pages[1][description]:[123123] pages[1][order]:[1] tags[0][id]:[12] tags[0][tag_name]:[sdf] title:[123123]]

我想将页面的数据作为数组获取,但我得到了[]

4

1 回答 1

0

您可以使用 3rd 方库。

import "github.com/monoculum/formam/v3"
type MyFormData struct {
    Pages []struct {
        Order       int    `formam:"order"`
        Description string `formam:"description"`
    } `formam:"pages"`
    Tags []struct {
        TagName string `formam:"tag_name"`
        Id      string `formam:"id"`
    } `formam:"tags"`
    Title string `formam:"title"`
}
func HttpHandler(c echo.Context) error {
    myFormData := MyFormData{}
    form, err := c.FormParams()

    if err != nil {
        return err
    }

    dec := formam.NewDecoder(&formam.DecoderOptions{TagName: "formam"})
    dec.Decode(form, &myFormData)

    return c.JSON(200, myFormData)
}
于 2021-10-05T11:43:31.577 回答