【发布时间】:2021-12-02 09:50:05
【问题描述】:
我正在尝试在基于 Gin 的 Web 服务器的路由中使用外部(非匿名)函数,如下所示:
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
router.GET("/hi/", Hi)
router.Run(":8080")
}
func (c *gin.Context) Hi() {
c.String(http.StatusOK, "Hello")
}
但我得到 2 个错误:
./main.go:13:23: undefined: Hi
./main.go:18:6: cannot define new methods on non-local type gin.Context
我想知道如何在带有 gin gonic 的端点处理程序中使用匿名函数?到目前为止,我发现的所有文档都使用匿名函数。
谢谢!
【问题讨论】: