回顾
上一节我们用Gin框架快速搭建了一个GET请求的接口,今天来学习路由和参数的获取。
请求动词
熟悉RESTful
的同学应该知道,RESTful
是网络应用程序的一种设计风格和开发方式,每一个URI代表一种资源,客户端通过POST
、DELETE
、PUT
、GET
四种请求方式来对资源做增删改查的操作。
同样的,Gin框架给我们提供的除这4种动词外,还有PATCH
、OPTION
、HEAD
等,详细内容可以查看rentergroup.go
文件的IRoutes
接口。
type IRoutes interface {
Use(...HandlerFunc) IRoutes
Handle(string, string, ...HandlerFunc) IRoutes
Any(string, ...HandlerFunc) IRoutes
GET(string, ...HandlerFunc) IRoutes
POST(string, ...HandlerFunc) IRoutes
DELETE(string, ...HandlerFunc) IRoutes
PATCH(string, ...HandlerFunc) IRoutes
PUT(string, ...HandlerFunc) IRoutes
OPTIONS(string, ...HandlerFunc) IRoutes
HEAD(string, ...HandlerFunc) IRoutes
StaticFile(string, string) IRoutes
Static(string, string) IRoutes
StaticFS(string, http.FileSystem) IRoutes
}
因为RenterGroup
实现了IRoutes
定义的所有请求动词,而且gin.Default
返回的Engine
类型继承了RenterGroup
,所以使用起来非常简单,只需要通过gin.Default
实例化对象,接下来所有的路由操作都通过该对象使用即可。
func main() {
router := gin.Default()
router.POST("/article", func(c *gin.Context) {
c.String(200, "article post")
})
router.DELETE("/article", func(c *g