gin最简单的实践:
func main() {
// 1.创建路由
r := gin.Default()
// 2.绑定路由规则,执行的函数
// gin.Context,封装了request和response
r.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "hello World!")
})
// 3.监听端口,默认在8080
// Run("里面不指定端口号默认为8080")
r.Run(":8000")
}
可以通过Context的Param方法来获取API参数
(也就是在我们输入url的时候,进行获取我们输入的url的内容)
func main() {
r := gin.Default()
r.GET("/user/:name/*action", func(c *gin.Context) {
name := c.Param("name")
action := c.Param("action")
//截取/
action = strings.Trim(action, "/")
c.String(http.StatusOK, name+" is "+action)
})
//默认为监听8080端口
r.Run(":8000")
}
下面是通过自带的方法来得到输入的url中的内容:
也就是说如果你输入http://localhost:8888/user?那就返回你自己设定好的,如果你http://localhost:8888/user?name=333这样,就返回你输入的。
func main() {
r := gin.Default()
r.GET("/user", func(c *gin.Context