Gin
快速开始 依赖引入 1 go get -u github.com/gin-gonic/gin 1 import "github.com/gin-gonic/gin" 创建服务 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 package main import ( "github.com/gin-gonic/gin" "net/http" ) func main() { engine := gin.Default() engine.GET("/", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{ "message": "Hello World!", }) }) engine.Run(":52100") } 框架基础 请求参数 路由参数 绑定路由参数时,使用:作为前缀,例如:paramName 通过context.Param("paramName")获取参数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 package main import ( "github.com/gin-gonic/gin" "log" "net/http" ) func main() { e := gin.Default() e.GET("/findUser/:username/:userid", FindUser) e.GET("/downloadFile/*filepath", UserPage) log.Fatalln(e.Run(":8080")) } // 命名参数示例 func FindUser(c *gin.Context) { username := c.Param("username") userid := c.Param("userid") c.String(http.StatusOK, "username is %s\n userid is %s", username, userid) } // 路径参数示例 func UserPage(c *gin.Context) { filepath := c.Param("filepath") c.String(http.StatusOK, "filepath is %s", filepath) } 请求参数 context.DefaultQuery("username", "defaultUser")获取请求参数,如果参数不存在则返回默认值 ...