1.engine實例的創建
func main(){
engine := gin.Default()
//定義個GET請求
/*engine.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "hello World!")
})*/
//定義通用方法 Handle
engine.Handle("GET","/hello", func(context *gin.Context) {
path := context.FullPath() //獲取請求路徑 /hello
fmt.Println(path)
name := context.DefaultQuery("name","zhangsan")
fmt.Println(name)
//頁面打印
context.Writer.Write([]byte("Hello ,"+name))
context.JSON(http.StatusOK,gin.H{"code":http.StatusOK,"msg":"操作成功"}) //返回 JSON
}) engine.Run() }
2.獲取參數
id := context.Query("id") //獲取URL后面的參數
name := context.DefaultQuery("name","張三豐") //獲取URL后面的參數 ,可以設置默認值
username := c.PostForm("username")//從表單中查詢參數
sex := c.DefaultPostForm("sex","男") //可以設置默認值
name := context.Params("name") //獲取URL綁定參數
