gin
gin copied to clipboard
Panic: runtime error: invalid memory address or nil pointer dereference
Description
When I using Gin http get request to get the result synchronously, the following panic occurrs.
How to reproduce
The problem occurs occasionally, so I can't locate where the problem is.Below is the brief code.
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
type Gin struct {
C *gin.Context
}
func (g *Gin) AgentResponse(httpCode int, status string, message string, data interface{}) {
g.C.JSON(httpCode, Response{
Status: status,
Message: message,
Detail: data,
})
}
func main() {
g := gin.Default()
g.GET("/hello/world", func(c *gin.Context) {
instanceId := c.DefaultQuery("InstanceId", "")
ugin := Gin{C: c}
ugin.AgentResponse(http.StatusOK, "Success", "", nil)
})
g.Run(":9000")
}
Expectations
$ curl http://localhost:9000/hello/world
{
"Detail": null,
"Message": "",
"Status": "Success"
}
Actual result
$ curl -i http://localhost:8201/hello/world
http: panic serving 10.0.9.53:54118: runtime error: invalid memory address or nil pointer dereference
...
As shown above.
Environment
- go version: 1.13
- gin version (or commit ref): 1.6.3
- operating system: centos 7.6
most likely your code is not working. there is no Response struct