是GO编程语言的新手。我正在使用gin-gonic框架构建一个Web服务器。我试图将req.body中的值映射到结构上。
使用Postman我在x-www-form-urlencoded
下发送一个带有以下键/ vaules的POST请求角色:admin
用户名:adminusername
id:1
我的代码如下
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") 但是当我尝试将它绑定到结构时,我得到以下错误
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
答案 0 :(得分:1)
我想,而不是
var reqBody requestBody
err := c.Bind(reqBody)
可能是
reqBody := new(requestBody)
err := c.Bind(reqBody)
第二个代码创建一个指向requestBody的指针,因此Bind可以应用于指针。如果你传递一个它可以绑定的值,但你不会得到结果。所以它是无效的类型。更具体地说,在mapForm: typ := reflect.TypeOf(ptr).Elem()
它采用类型,假设它是指向某事物的指针(Elem())。传递值Elem()
恐慌时,请参阅https://golang.org/pkg/reflect/#Value.Elem
为什么new()
而不是var reqBody *requestBody
。 new
将创建一个新对象来绑定值。在第二种情况下声明指针将创建一个nil
指针,不可绑定。
你也可以写reqBody := &requestBody{}
,这与new
语法相同