[TOC] > [官方文档](https://gin-gonic.com/zh-cn/docs/) ## 安装 `go get -u github.com/gin-gonic/gin` ## 接口 ### 获取 get / post / file 参数 main.go ``` package main import "github.com/gin-gonic/gin" func main() { router := gin.Default() // Param 获取 :name 的值 router.GET("/user/:name", func(c *gin.Context) { name := c.Param("name") c.String(http.StatusOK, "Hello %s", name) }) 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.POST("/user/:name/*action", func(c *gin.Context) { c.FullPath() == "/user/:name/*action" // true }) //获取 get 参数 router.GET("/welcome", func(c *gin.Context) { firstname := c.DefaultQuery("firstname", "Guest") lastname := c.Query("lastname") // 等于 c.Request.URL.Query().Get("lastname") c.String(http.StatusOK, "Hello %s %s", firstname, lastname) }) //获取 form 表单的值 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.POST("/upload", func(c *gin.Context) { // router.MaxMultipartMemory = 8 << 20 // 8 MiB (default is 32 MiB) file, _ := c.FormFile("file") log.Println(file.Filename) // c.SaveUploadedFile(file, dst) c.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!", file.Filename)) }) //多文件上传 router.POST("/upload", func(c *gin.Context) { form, _ := c.MultipartForm() files := form.File["upload[]"] for _, file := range files { log.Println(file.Filename) // c.SaveUploadedFile(file, dst) } c.String(http.StatusOK, fmt.Sprintf("%d files uploaded!", len(files))) }) router.Run(":8080") } ``` `go run main.go` ## 路由 ### GET / POST / DELETE ``` router.GET("/someGet", getting) router.Any("/someGet", getting) router.POST("/somePost", posting) router.PUT("/somePut", putting) router.DELETE("/someDelete", deleting) router.PATCH("/somePatch", patching) router.HEAD("/someHead", head) router.OPTIONS("/someOptions", options) ``` ### Group 路由 ``` v1 := router.Group("/v1") { v1.GET("/login", loginEndpoint) v1.POST("/submit", submitEndpoint) v1.POST("/read", readEndpoint) } ``` ### 嵌套 group 路由 ``` v1 := router.Group("/v1") { v1.GET("/login", loginEndpoint) user := v1.Group("/user") { user.GET("/get", demo) } } ``` ## 多格式渲染 ``` c.JSON(http.StatusOK, gin.H{ "user": "rsj217", "passwd": "123", }) c.XML(http.StatusOK, gin.H{ "user": "rsj217", "passwd": "123", }) ``` ## 重定向 ``` router.GET("/redict/google", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "https://google.com") }) ``` ## 异步协程 ``` router.GET("/sync", func(c *gin.Context) { time.Sleep(5 * time.Second) log.Println("Done! in path" + c.Request.URL.Path) }) router.GET("/async", func(c *gin.Context) { //请求的上下文需要copy到异步的上下文,并且这个上下文是只读的 cCp := c.Copy() go func() { time.Sleep(5 * time.Second) log.Println("Done! in path" + cCp.Request.URL.Path) }() }) ``` ## 设置静态资源 ``` router.Static("/assets", "./assets") router.StaticFS("/more_static", http.Dir("my_file_system")) router.StaticFile("/favicon.ico", "./resources/favicon.ico") ```