1. gin 获取
go get -u github.com/gin-gonic/gin
1.1 使用 gin.Default() 获取引擎,当然使用 gin.New() 也可以。
1.2 gin.Default() 其中已经附加了记录器和恢复中间件。
1.3 gin.New() 是一个空白的引擎。
1.4 入门代码 demo
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func helloGin(context *gin.Context) {
context.JSON(http.StatusOK, gin.H{
"code": 200,
"mesage": "hello gin",
})
}
func getTest(context *gin.Context) {
no := context.Query("no")
name := context.Query("name")
context.JSON(http.StatusOK, gin.H{
"no": no,
"name": name,
})
}
func postTest(context *gin.Context) {
no := context.PostForm("no")
name := context.PostForm("name")
context.JSON(http.StatusOK, gin.H{
"no": no,
"name": name,
})
}
func main() {
engine := gin.Default()
engine.GET("/helloGin", helloGin)
engine.GET("/getTest", getTest)
engine.POST("/postTest", postTest)
engine.Run()
}
2. gin 路由分组
2.1 使用 Group() 方法分组
2.2 代码 demo
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
v1 := router.Group("/v1")
v1.GET("/hello", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"code": 200,
"message": "hello v1",
})
})
v2 := router.Group("/v2")
v2.GET("/hello", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"code": 200,
"message": "hello v2",
})
})
v2.GET("/test", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"code": 200,
"message": "hello v2 test",
})
})
router.Run()
}
3. gin 上传文件
3.1 单个文件使用:FormFile()
3.2 多个文件使用:MultipartForm()
3.3 保存文件使用:SaveUploadedFile()
3.4 代码 demo
package main
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
func uploadFile(c *gin.Context) {
loadFile, err := c.FormFile("file")
if err != nil {
fmt.Printf("uploadFile err:%v", err)
return
}
err = c.SaveUploadedFile(loadFile, "c:/tmp/"+loadFile.Filename)
if err != nil {
fmt.Printf("SaveUploadedFile err:%v", err)
c.JSON(http.StatusOK, gin.H{
"message": "SaveUploadedFile err",
})
} else {
c.JSON(http.StatusOK, gin.H{
"message": "SaveUploadedFile succ",
})
}
}
func uploadFiles(c *gin.Context) {
form, err := c.MultipartForm()
if err != nil {
fmt.Printf("uploadFiles err :%v", err)
return
}
files := form.File["file"]
for _, v := range files {
c.SaveUploadedFile(v, "c:/tmp/"+v.Filename)
}
c.JSON(http.StatusOK, gin.H{
"uploadFiel_Total": len(files),
})
}
func main() {
router := gin.Default()
router.POST("/uploadFile", uploadFile)
router.POST("/uploadFiles", uploadFiles)
router.Run()
}
4. gin 处理 json 请求
4.1 使用 ShouldBindJSON() 处理接受到的 json
4.2 代码 demo
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
type book struct {
ID int64 `json:"id" `
Name string `json:"name"`
Price float64 `json:"price"`
}
func jsonTest(c *gin.Context) {
b := new(book)
err := c.ShouldBindJSON(b)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"code": "9999",
"message": err,
})
} else {
c.JSON(http.StatusOK, gin.H{
"code": "0000",
"book_id": b.ID,
"book_name": b.Name,
"book_price": b.Price,
})
}
}
func main() {
router := gin.Default()
router.POST("/jsonTest", jsonTest)
router.Run()
}
5. gin handler 使用
5.1 创建一个返回值为 gin.HandlerFunc 的函数
5.2 使用 gin.Engine 的 Use() 方法调用函数
5.3 代码 demo
package main
import (
"log"
"net/http"
"time"
"github.com/gin-gonic/gin"
)
func test() gin.HandlerFunc {
return func(c *gin.Context) {
log.Println("start")
t := time.Now()
c.Next()
log.Printf("use time %v", time.Since(t))
}
}
func main() {
router := gin.Default()
router.Use(test())
router.GET("/testHandler", func(c *gin.Context) {
time.Sleep(2 * time.Second)
c.JSON(http.StatusOK, gin.H{
"code": 200,
})
})
router.Run()
}