gin icon indicating copy to clipboard operation
gin copied to clipboard

Panic: runtime error: invalid memory address or nil pointer dereference

Open wywself opened this issue 1 year ago • 1 comments

Description

When I using Gin http get request to get the result synchronously, the following panic occurrs. 1659408791805

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

wywself avatar Aug 02 '22 03:08 wywself

most likely your code is not working. there is no Response struct

Gasoid avatar Sep 07 '22 18:09 Gasoid