一、gin入门

1. 介绍

  • 微框架
  • 速度快

2. 安装

go get –u github.com/gin-gonic/gin

3. HelloWorld

engine := gin.New()
engine.GET("/", func(g *gin.Context) {
	g.String(http.StatusOK,"hello world")
})
engine.Run()
二、gin路由

1. 路由器

  • gin框架张采用的路由库是基于httprouter做的
  • 地址为 github.com/julienschmidt/httprouter

2. Restful风格的API

  • gin支持Restful风格的API
  • Restful风格: URL定位资源,用HTTP描述操作

3. API参数

//创建默认路由
r := gin.Default()
//api参数
r.GET("/user/:name/*action", func(c *gin.Context) {
	//
	name := c.Param("name")
	action := c.Param("action")
	c.String(http.StatusOK,name + " is " + action )
})
r.Run()
localhost:8080/user/henry/man
henry is /man

4. URL参数

//创建默认路由
r := gin.Default()
//api参数
r.GET("/welcome", func(c *gin.Context) {
	//
	name := c.DefaultQuery("name", "Henry")
	c.String(http.StatusOK,fmt.Sprintf("Hello %s",name))
})
r.Run()
localhost:8080/welcome
Hello Henry

5. 表单参数

//创建默认路由
r := gin.Default()
//api参数
r.POST("/form", func(c *gin.Context) {
	// 表单参数设置默认值
	type1 := c.DefaultPostForm("type", "alice")

	username := c.PostForm("username")
	pwd := c.PostForm("password")
	// 多选框
	hobby := c.PostFormArray("hobby")

	c.String(http.StatusOK, fmt.Sprintf("type is %s,name is %s,"+
		"password is %s,hobbys is %v", type1, username, pwd, hobby))
})
r.Run()
 <form action="http://127.0.0.1:8080/form" method="post" enctype="application/x-www-form-urlencoded">
		user&nbsp&nbsp:<input type="text" name="username">
		<br>
		pwd&nbsp&nbsp:<input type="password" name="password">
		<br>
		hobby:<input type="checkbox" value="run" name="hobby">跑步
		<input type="checkbox" value="game" name="hobby">游戏
		<input type="checkbox" value="money" name="hobby">金钱
		<br>
		<input type="submit" value="登录">
</form>
type is alice,name is henry,password is 123456,hobbys is []

6. 上传单个文件

//创建默认路由
r := gin.Default()

//限制表单上传大小 默认为32MB
r.MaxMultipartMemory = 8 << 20

r.POST("/upload", func(c *gin.Context) {
	//表单取文件
	file ,_ := c.FormFile("file")
	log.Println(file.Filename)

	// 传到项目的根目录,名字就用本身的
	_ 	= c.SaveUploadedFile(file, file.Filename)

	// 打印信息
	c.String(http.StatusOK,fmt.Sprintf("'%s' upload"),file.Filename)
})
r.Run()
<form action="http://127.0.0.1:8080/upload" method="post" enctype="multipart/form-data">
	image:
	<input type="file" name="file">
	<br>
	<input type="submit" value="登录">
</form>

7. 上传多个文件

后台代码

	//创建默认路由
	r := gin.Default()
	//限制表单上传大小 默认为32MB
	r.MaxMultipartMemory = 8 << 20
	
	r.POST("/upload", func(c *gin.Context) {
		form, err := c.MultipartForm()
		if err != nil {
			c.String(http.StatusBadRequest, fmt.Sprintf("err is %s", err))
		}
	
		// 获取所有图片
		files := form.File["files"]
		for _, file := range files {
			// 逐个存
			if err := c.SaveUploadedFile(file, file.Filename); err != nil {
				c.String(http.StatusBadRequest, fmt.Sprintf("upload err %s", err))
				return
			}
		}
	
		c.String(http.StatusOK, "upload ok %d files", len(files))
	})
	r.Run()

前台代码

	<form action="http://127.0.0.1:8080/upload" method="post" enctype="multipart/form-data">
	    image:
	    <input type="file" name="files" multiple>
	    <br>
	    <input type="submit" value="提交">
	</form>

返回

	upload ok 2 files

8. routers group

代码

	func main() {
		//创建默认路由
		r := gin.Default()
		v1 := r.Group("/v1")
		// {} 规范格式 不影响结果
		{
			v1.GET("/login",login)
			v1.GET("/submit",submit)
		}
		v2 := r.Group("/v2")
		{
			v2.POST("/login",login)
			v2.POST("/submit",submit)
		}
		r.Run()
	}
	func login(c *gin.Context){
		name := c.DefaultQuery("name", "henry")
		c.String(http.StatusOK,fmt.Sprintf("hello %s\n",name))
	}
	func submit(c *gin.Context){
		name := c.DefaultQuery("name", "adobe")
		c.String(http.StatusOK,fmt.Sprintf("hello %s\n",name))
	}

9. 路由原理

  • httprouter会将所有的路由规则构造一棵前缀树