GET, POST, PUT, PATCH, DELETE, OPTIONS
func testfunc(ctx *gin.Context) { ctx.JSON(http.StatusOK,gin.H{ "ping":"pong", })}func main() { // 禁用控制台颜色 // gin.DisableConsoleColor() // 使用默认中间件创建一个 gin 路由: // 日志与恢复中间件。 router := gin.Default() router.GET("/someGet", testfunc) router.POST("/somePost", testfunc) router.PUT("/somePut", testfunc) router.DELETE("/someDelete", testfunc) router.PATCH("/somePatch", testfunc) router.HEAD("/someHead", testfunc) router.OPTIONS("/someOptions", testfunc) // 默认情况下,它使用:8080,除非定义了 PORT 环境变量。 router.Run() // router.Run(":3000") 硬编码端口}
path中的参数
func main() { router := gin.Default() // 这个处理器可以匹配 /user/john , 但是它不会匹配 /user router.GET("/user/:name", func(c *gin.Context) { name := c.Param("name") c.String(http.StatusOK, "Hello %s", name) }) // 但是,这个可以匹配 /user/john 和 /user/john/send // 如果没有其他的路由匹配 /user/john , 它将重定向到 /user/john/ router.GET("/user/:name/*action", func(c *gin.Context) { name := c.Param("name") action := c.Param("action") message := name + " is " + action c.String(http.StatusOK, message) }) router.Run(":8080")}
查询字符串参数
func main() { router := gin.Default() // 请求响应匹配的 URL: /welcome?firstname=Jane&lastname=Doe router.GET("/welcome", func(c *gin.Context) { firstname := c.DefaultQuery("firstname", "Guest") // 这个是 c.Request.URL.Query().Get("lastname") 的快捷方式。 lastname := c.Query("lastname") c.String(http.StatusOK, "Hello %s %s", firstname, lastname) }) router.Run(":8080")}
Multipart/Urlencoded 表单
func main() { router := gin.Default() router.POST("/form_post", func(c *gin.Context) { message := c.PostForm("message") nick := c.DefaultPostForm("nick", "anonymous") c.JSON(200, gin.H{ "status": "posted", "message": message, "nick": nick, }) }) router.Run(":8080")}
查询字符串参数 + post 表单
POST /post?id=1234&page=1 HTTP/1.1Content-Type: application/x-www-form-urlencodedname=manu&message=this_is_great
func main() { router := gin.Default() router.POST("/post", func(c *gin.Context) { id := c.Query("id") page := c.DefaultQuery("page", "0") name := c.PostForm("name") message := c.PostForm("message") fmt.Printf("id: %s; page: %s; name: %s; message: %s", id, page, name, message) }) router.Run(":8080")}
id: 1234; page: 1; name: manu; message: this_is_great
上传文件
单文件
func main() { router := gin.Default() // 为 multipart 表单设置一个较低的内存限制(默认是 32 MiB) // router.MaxMultipartMemory = 8 << 20 // 8 MiB router.POST("/upload", func(c *gin.Context) { file, _ := c.FormFile("file") log.Println(file.Filename) // 上传文件到指定的 dst 。 // c.SaveUploadedFile(file, dst) c.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!", file.Filename)) }) router.Run(":8080")}
多文件
func main() { router := gin.Default() // 为 multipart 表单设置一个较低的内存限制(默认是 32 MiB) // router.MaxMultipartMemory = 8 << 20 // 8 MiB router.POST("/upload", func(c *gin.Context) { // Multipart form form, _ := c.MultipartForm() files := form.File["upload[]"] for _, file := range files { log.Println(file.Filename) // 上传文件到指定的 dst. // c.SaveUploadedFile(file, dst) } c.String(http.StatusOK, fmt.Sprintf("%d files uploaded!", len(files))) }) router.Run(":8080")}