• With issues:
  • [x] Use the search tool before opening a new issue.

Description

The output of the HandlerName() function depends on go version.

How to reproduce

main.go

package main

import (
    "log"
    "test/handlers"

    "github.com/gin-gonic/gin"
)

func main() {
    r := gin.Default()

    r.Use(func(c *gin.Context) {
        c.Next()
        log.Println(c.HandlerName())
    })

    r.GET("/ping", handlers.PingHandler())

    r.Run()
}

handlers/pingHandler.go

package handlers

import (
    "net/http"

    "github.com/gin-gonic/gin"
)

func PingHandler() gin.HandlerFunc {
    return func(c *gin.Context) {
        c.JSON(http.StatusOK, gin.H{"message": "pong"})
    }

}

Dockerfile

FROM golang:1.21 as builder
WORKDIR /app
RUN go mod init test
RUN go get github.com/gin-gonic/gin@v1.9.1
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -pgo=off -o main .

FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=builder /app/main .
EXPOSE 8080

CMD ["./main"]

Request:

curl 0.0.0.0:8080/ping

Expectations

The output of the HandlerName function is consistent between go versions.

Actual result

using golang:1.21 image shows the output of log.Println(c.HandlerName()) as

main.main.PingHandler.func2

using golang:1.20 image shows the output of log.Println(c.HandlerName()) as

test/handlers.PingHandler.func1

Comment From: doriotp

Hi @asamokhina is this issue still open?

Comment From: ghosx

I Guess it's because PingHandler is inlined, you can try adding -gcflags="-l" when building. @asamokhina

Comment From: asamokhina

Thanks! Adding -gcflags="-l" when building resolves the issue.