从 Go 将 json 转换为结构时出错

Error while converting json to struct from Go

 func MakeMap(w http.ResponseWriter, r *http.Request) {
    // userInfo := context.Get(r, "userInfo").(model.User)
    type _getData struct {
        Title string   `json:"title"`
        Tag   []string `json:"tag"`
    }
    var getData _getData
    err := json.NewDecoder(r.Body).Decode(&getData)
    if err != nil {
        panic(err.Error())
    }
    fmt.Print(getData)

}

当我运行上面的代码时,我得到以下错误

2021/08/24 13:56:54 http: panic serving 127.0.0.1:50619: runtime error: invalid memory address or nil pointer dereference
goroutine 23 [running]:
net/http.(*conn).serve.func1(0x140001e9180)
        /usr/local/go/src/net/http/server.go:1824 +0x108
panic(0x10505b860, 0x10522f240)
        /usr/local/go/src/runtime/panic.go:971 +0x3f4
traveling/controller/mapController.MakeMap(0x1050b5630, 0x140001f40e0, 0x1400018aa00)
/Users/choeyunseog/traveling/traveling/controller/mapController/mapController.go:20 +0x3c

刚开始学习,不知道为什么会遇到这个问题,求助

err := json.NewDecoder(r.Body).Decode(&getData) 

当我像上面那样更改代码行 20 时出现以下错误

 2021/08/24 14:16:44 http: panic serving 127.0.0.1:51396: invalid character '-' in numeric literal
goroutine 23 [running]:
net/http.(*conn).serve.func1(0x140001e9360)
        /usr/local/go/src/net/http/server.go:1824 +0x108
panic(0x100d85d00, 0x14000206070)
        /usr/local/go/src/runtime/panic.go:971 +0x3f4
traveling/controller/mapController.MakeMap(0x100df1630, 0x140001f40e0, 0x1400018aa00)
        /Users/choeyunseog/traveling/traveling/controller/mapController/mapController.go:24 +0x194
net/http.HandlerFunc.ServeHTTP(0x100de75d8, 0x100df1630, 0x140001f40e0, 0x1400018aa00)
        /usr/local/go/src/net/http/server.go:2069 +0x40

要从 POST/PUT/PATCH 请求正文中获取多部分表单数据,您可以使用 ParseMultipartForm method to parse the body and then access the data through the PostForm field. Or you can use FormValue 获取与表单字段关联的第一个值。

maxMemory := 32<<20
if err := r.ParseMultipartForm(maxMemory); err != nil {
    panic(err)
}

fmt.Println(_getData{
    Title: r.FormValue("title"), // FormValue returns string
    Tag:   r.PostForm["tag[]"],  // PostForm is a map of []string
})

您可以使用包 github.com/senpathi/paramex 将表单数据解析为 json,就像带注释的结构一样。结构字段必须用 param 关键字注释,标签名称是表单数据的键。

你的结构应该如下所示。

type _getData struct {
        Title string   `param:"title"`
        Tag   []string `param:"tag[]"`
    }

这是针对问题中提到的邮递员请求更新后的 MakeMap 处理函数

func MakeMap(w http.ResponseWriter, r *http.Request) {
    // userInfo := context.Get(r, "userInfo").(model.User)
    type _getData struct {
        Title string   `param:"title"`
        Tag   []string `param:"tag[]"`
    }

    // this needed because u send data from Postman as multipart/form-data
    maxMemory := 32<<20
    if err := r.ParseMultipartForm(int64(maxMemory)); err != nil {
        panic(err)
    }

    var getData _getData

    extractor := paramex.NewParamExtractor()
    err := extractor.ExtractForms(&getData, r)
    if err != nil {
        panic(err.Error())
    }

    fmt.Print(getData)
    //Output: {defaultMap [travelling travelling2]}
}