package mainimport ("fmt""net/http""/gin-gonic/gin")func main() {r := gin.Default()// GET 获取所有的文章信息r.GET("/posts", func(c *gin.Context) {c.String(http.StatusOK, "GET")})// POST 创建一篇新文章r.POST("/posts", func(c *gin.Context) {c.String(http.StatusOK, "POST")})// PUT 修改一篇文章r.PUT("/posts/:id", func(c *gin.Context) {c.String(http.StatusOK, fmt.Sprintf("PUT id: %s", c.Param("id")))})// DELETE 删除一篇文件r.DELETE("/posts/", func(c *gin.Context) {c.String(http.StatusOK, "DELETE")})// 匹配所有请求方法/*MethodGet= "GET"MethodHead = "HEAD"MethodPost = "POST"MethodPut= "PUT"MethodPatch = "PATCH" // RFC 5789MethodDelete = "DELETE"MethodConnect = "CONNECT"MethodOptions = "OPTIONS"MethodTrace = "TRACE"*/r.Any("/users", func(c *gin.Context) {c.String(200, "any")})r.Run()}
如果觉得《Gin的路由类型:GET POST PUT DELETE》对你有帮助,请点赞、收藏,并留下你的观点哦!