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)
	}
}


上一篇:Gin 路由分组(嵌套)


下一篇:gin验证器使用中文返回