我是 GO 编程语言的新手。我正在使用 gin-gonic 框架构建 Web 服务器。我正在尝试将 req.body 中的值映射到一个结构上。
我使用 Postman 在 x-www-form-urlencoded 下发送带有以下键/值的 POST 请求
角色:管理员
用户名:管理员用户名
编号:1
我的go代码如下
package jwtsecuritytoken
import (
"fmt"
"github.com/gin-gonic/gin"
)
type requestBody struct {
role string
username string
id string
}
func GenerateToken(c *gin.Context) {
fmt.Println(c.PostForm("role"))
var reqBody requestBody
err := c.Bind(reqBody)
if err != nil {
fmt.Println(err)
}
fmt.Println(reqBody)
content := gin.H{"Hello": "World"}
c.JSON(200, content)
}
我能够从 c.PostForm("name") 读取值 但是当我尝试将它绑定(bind)到结构时,出现以下错误
reflect: Elem of invalid type
C:/Go/src/runtime/panic.go:491 (0x42bf80)
gopanic: reflectcall(nil, unsafe.Pointer(d.fn), deferArgs(d), uint32(d.siz), uint32(d.siz))
C:/Go/src/reflect/type.go:955 (0x4bdb19)
(*rtype).Elem: panic("reflect: Elem of invalid type")
C:/Learnings/Go/src/github.com/gin-gonic/gin/binding/form_mapping.go:15 (0x8b6b5a)
mapForm: typ := reflect.TypeOf(ptr).Elem()
C:/Learnings/Go/src/github.com/gin-gonic/gin/binding/form.go:24
formBinding.Bind: if err := mapForm(obj, req.Form); err != nil {
<autogenerated>:1 (0x8b9298)
C:/Learnings/Go/src/github.com/gin-gonic/gin/context.go:508 (0x8f3319)
(*Context).ShouldBindWith: return b.Bind(c.Request, obj)
C:/Learnings/Go/src/github.com/gin-gonic/gin/context.go:475 (0x8f2e3d)
(*Context).MustBindWith: if err = c.ShouldBindWith(obj, b); err != nil {
C:/Learnings/Go/src/github.com/gin-gonic/gin/context.go:458 (0x8f2a7d)
(*Context).Bind: return c.MustBindWith(obj, b)
C:/Learnings/Go/src/AuthenticateService/jwtsecuritytoken/jwtsecuritytoken.go:22 (0x905c23)
GenerateToken: err := c.Bind(reqBody)
C:/Learnings/Go/src/github.com/gin-gonic/gin/context.go:107 (0x8f10b9)
(*Context).Next: c.handlers[c.index](c)
C:/Learnings/Go/src/github.com/gin-gonic/gin/recovery.go:46 (0x9038f0)
RecoveryWithWriter.func1: c.Next()
C:/Learnings/Go/src/github.com/gin-gonic/gin/context.go:107 (0x8f10b9)
(*Context).Next: c.handlers[c.index](c)
C:/Learnings/Go/src/github.com/gin-gonic/gin/logger.go:83 (0x902c12)
LoggerWithWriter.func1: c.Next()
C:/Learnings/Go/src/github.com/gin-gonic/gin/context.go:107 (0x8f10b9)
(*Context).Next: c.handlers[c.index](c)
C:/Learnings/Go/src/github.com/gin-gonic/gin/gin.go:352 (0x8fa52c)
(*Engine).handleHTTPRequest: c.Next()
C:/Learnings/Go/src/github.com/gin-gonic/gin/gin.go:319 (0x8f9cb1)
(*Engine).ServeHTTP: engine.handleHTTPRequest(c)
C:/Go/src/net/http/server.go:2619 (0x66b69a)
serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
C:/Go/src/net/http/server.go:1801 (0x6676c3)
(*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
C:/Go/src/runtime/asm_amd64.s:2337 (0x457e70)
goexit: BYTE $0x90 // NOP
最佳答案
我猜,而不是
var reqBody requestBody
err := c.Bind(reqBody)
可能是
reqBody := new(requestBody)
err := c.Bind(reqBody)
第二个代码创建一个指向 requestBody 的指针,因此 Bind 可以应用于一个指针。如果你传递一个值,它可以绑定(bind),但你不会得到结果。所以它是无效类型。更具体地说,在 mapForm: typ := reflect.TypeOf(ptr).Elem()
它采用类型,假设它是指向某物 (Elem()) 的指针。当你传入一个值时 Elem()
panic 见https://golang.org/pkg/reflect/#Value.Elem
为什么是 new()
而不是 var reqBody *requestBody
。 new
将创建一个新对象来绑定(bind)值。在第二种情况下声明指针将创建一个不可绑定(bind)的 nil
指针。
您也可以编写 reqBody := &requestBody{}
,这与 new
语法相同
关于go - gin-gonic 将 request.body 值映射到结构中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47844182/