Golang Gin 框架获取 HTTP 方法名示例
在 Golang Gin 框架中,可以通过 'c.Request.Method' 来获取 HTTP 请求的方法名。以下是一个示例:
package main
import (
"fmt"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET('/hello', func(c *gin.Context) {
method := c.Request.Method
fmt.Println('Method:', method)
})
r.Run(':8080')
}
在上面的示例中,我们定义了一个 GET 请求的路由 '/hello',当访问该路由时,会执行匿名函数。在函数中,我们通过 'c.Request.Method' 获取到 HTTP 请求的方法名,并打印出来。
运行程序后,访问 'http://localhost:8080/hello',控制台会输出 'Method: GET',表示成功获取到了 GET 方法名。
原文地址: https://www.cveoy.top/t/topic/fB9T 著作权归作者所有。请勿转载和采集!