其他分享
首页 > 其他分享> > Gin Any方法路由

Gin Any方法路由

作者:互联网

package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

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

	router.Any("/", func(ctx *gin.Context) {
		switch ctx.Request.Method {
		case http.MethodGet:
			ctx.JSON(http.StatusOK, gin.H{"Method": http.MethodGet})
		case http.MethodPost:
			ctx.JSON(http.StatusOK, gin.H{"Method": http.MethodPost})
		case http.MethodPut:
			ctx.JSON(http.StatusOK, gin.H{"Method": http.MethodPut})
		case http.MethodDelete:
			ctx.JSON(http.StatusOK, gin.H{"Method": http.MethodDelete})
		}
	})

	err := router.Run(":8080")
	if err != nil {
		panic(err)
	}
}


标签:http,ctx,Method,JSON,StatusOK,gin,Gin,Any,路由
来源: https://www.cnblogs.com/liy36/p/15202318.html