/home/wj/gocode/src/gin/main.go
package main
import (
    "net/http"
    "strings"
    "github.com/gin-gonic/gin"
    "fmt"
)
func main() {
   // 创建路由
   r := gin.Default()
   
   // 网站主页,绑定路由规则,执行的函数
   // gin.Context,封装了request和response
   r.GET("/", func(c *gin.Context) {
      c.String(http.StatusOK, "hello World!")
   })
   // GET请求,usl是参数一部分
   // 127.0.0.1:8001/user/wangjing/adduser
   r.GET("/user/:name/*action", func(c *gin.Context) {
        name := c.Param("name")
        action := c.Param("action")
        //截取/
        action = strings.Trim(action, "/")
        c.String(http.StatusOK, name+" is "+action)
    })
	// GET请求参数使用?获取,设置默认参数值
	// 127.0.0.1:8001/user2?name=wangjing
	r.GET("/user2", func(c *gin.Context){
		name := c.DefaultQuery("name", "默认名字")
		c.String(http.StatusOK, fmt.Sprintf("hello %s", name))
	})
	// POST提交表单
	r.POST("/form", func(c *gin.Context){
		types := c.DefaultPostForm("type", "post")
		username := c.PostForm("username")
		password := c.PostForm("userpassword")
		c.String(http.StatusOK, fmt.Sprintf("username:%s, password:%s,type:%s",username,password,types))
	})
	// POST上传一个.txt文件:web页面 
	// 127.0.0.1:8001/index
	r.LoadHTMLGlob("views/index.html")
	r.GET("/index", func(c *gin.Context) {
        c.HTML(http.StatusOK, "index.html", nil)
    })
	// POST上传一个.txt文件:服务端处理逻辑
	r.POST("/upload", func(c *gin.Context) {
   		f, err := c.FormFile("f1")
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{
				"error": err,
			})
			return
		} else {
			c.SaveUploadedFile(f, "./wenjian/"+f.Filename)
			c.JSON(http.StatusOK, gin.H{
				"message": "OK",
			})
		}
	})
	// POST上传一个.txt文件:web页面
	// 127.0.0.1:8001/index2
	r.LoadHTMLGlob("views/index2.html")
	r.GET("/index2", func(c *gin.Context) {
		c.HTML(http.StatusOK, "index2.html", nil)
	})  
	// POST多文件上传
	// 127.0.0.1/uploadMore
	// 限制表单上传大小 8MB,默认为32MB
	r.MaxMultipartMemory = 8 << 20
	r.POST("/uploadMore", func(c *gin.Context) {
		form, err := c.MultipartForm()
		if err != nil {
			c.String(http.StatusBadRequest, fmt.Sprintf("get err %s", err.Error()))
		}
		// 获取所有图片
		files := form.File["files"]
		// 遍历所有图片
		for _, file := range files {
			// 逐个存
			if err := c.SaveUploadedFile(file, "./wenjian/"+file.Filename); err != nil {
				c.String(http.StatusBadRequest, fmt.Sprintf("upload err %s", err.Error()))
				return
			}
		}
		c.String(200, fmt.Sprintf("upload ok %d files", len(files)))
	})
	
   r.Run(":8001")
}# 单文件上传web页面,127.0.0.1/index
/home/wj/gocode/src/gin/views/index.go
<html> <head></head> <body> <form method="post" action="/upload" enctype="multipart/form-data"> file:<input type="file" name="f1"> <input type="submit" value="Commit"> </form> </body>
# 多文件上传web页面,127.0.0.1/index2
/home/wj/gocode/src/gin/views/index2.go
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <form action="/uploadMore" method="post" enctype="multipart/form-data"> 上传文件:<input type="file" name="files" multiple> <input type="submit" value="提交"> </form> </body> </html>
(完)