Home > front end >  Please tell me how to bind multi-array to struct
Please tell me how to bind multi-array to struct

Time:10-06

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

Receive the following data through c.FormParams command, please tell me how to bind it to _getData struct,

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

I want to get the data of pages as an array, but I am getting []

CodePudding user response:

You can use 3rd party lib.

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)
}
  • Related