Gin框架初探

伴随Go的日益普及,web框架也日益成熟,可选渐多,fasthttp让大家看到了Go的性能强大,echo、Gin等框架也以高性能著称,其中Gin最为热门,这里初探一下Gin的使用。

QuickStart

建立一个空目录,比如名叫ginweb,然后创建文件main.go :

package main

import (
	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	r.GET("/", func(c *gin.Context) {
		c.String(200, "Hello")
	})
	r.GET("/ping", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "pong",
		})
	})
	r.Run() // listen and serve on 0.0.0.0:8080
}

建立文件go.mod :

module ginweb

go 1.16

require github.com/gin-gonic/gin v1.6.3 // indirect

然后执行:

go get
go build
./ginweb

然后浏览器即可打开查看:http://localhost:8080/ping

参数解析

type Person struct {
	Name    string `form:"name" json:"name" uri:"name"`
	Address string `form:"address" json:"address" uri:"address"`
}

r.GET("/person", func(c *gin.Context) {
		var person Person
		if c.ShouldBind(&person) == nil {
			log.Println("====== Bind By Query String ======")
			log.Println(person.Name)
			log.Println(person.Address)
		}
		c.String(200, "Success: "+person.Name+" "+person.Address)
})
r.GET("/person/:name/:address", func(c *gin.Context) {
		var person Person
		if c.ShouldBindUri(&person) == nil {
			log.Println("====== Bind By Uri ======")
			log.Println(person.Name)
			log.Println(person.Address)
		}
		c.String(200, "Success Uri: "+person.Name+" "+person.Address)
})

HTML模版

router.LoadHTMLGlob("templates/*")
//router.LoadHTMLFiles("templates/template1.html", "templates/template2.html")
router.GET("/hello", func(c *gin.Context) {
  	var name String `form:"name"`
  	c.BindDefault(&naem, "World")
		c.HTML(http.StatusOK, "index.tmpl", gin.H{
			"title": name,
		})
})

建立模版文件:templates/index.tmpl

<html>
	<h1>
		{{ .title }}
	</h1>
</html>

测试代码

package main

import (
	"log"
	"net/http"

	"github.com/gin-gonic/gin"
)

type Person struct {
	Name    string `form:"name" json:"name" uri:"name"`
	Address string `form:"address" json:"address" uri:"address"`
}

func main() {
	r := gin.Default()
	r.GET("/", func(c *gin.Context) {
		c.String(200, "Hello")
	})
	r.LoadHTMLGlob("templates/*")
	//router.LoadHTMLFiles("templates/template1.html", "templates/template2.html")
	r.GET("/hello", func(c *gin.Context) {
		name := c.Query("name")
		log.Println("get name: " + name)
		c.HTML(http.StatusOK, "index.tmpl", gin.H{
			"title": name,
		})
	})
	r.GET("/ping", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "pong",
		})
	})
	r.GET("/person", func(c *gin.Context) {
		var person Person
		if c.ShouldBind(&person) == nil {
			log.Println("====== Bind By Query String ======")
			log.Println(person.Name)
			log.Println(person.Address)
		}
		c.String(200, "Success: "+person.Name+" "+person.Address)
	})
	r.GET("/person/:name/:address", func(c *gin.Context) {
		var person Person
		person.Address = c.Param("address")
		person.Name = c.Params.ByName("name")
		// the follow is the same
		/* if c.ShouldBindUri(&person) == nil {
			log.Println("====== Bind By Uri ======")
			log.Println(person.Name)
			log.Println(person.Address)
		} */
		c.String(200, "Success Uri: "+person.Name+" "+person.Address)
	})
	r.Run() // listen and serve on 0.0.0.0:8080
}

测试URL:

http://localhost:8080/
http://localhost:8080/ping
http://localhost:8080/hello?name=gin
http://localhost:8080/person?name=gin&address=earth
http://localhost:8080/person/gin/earth

附录