第一个Gin项目 1 2 3 4 5 6 7 8 package mainimport ( "github.com/gin-gonic/gin" ) func main () { e := gin.Default() e.Run() }
go程序 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 package mainimport ( "github.com/gin-gonic/gin" ) func gologin (c *gin.Context) { c.HTML(200 , "login.html" , nil ) } func login (c *gin.Context) { username := c.PostForm("username" ) password := c.PostForm("password" ) c.HTML(200 , "index.html" , gin.H{ "username" : username, "password" : password, }) } func main () { e := gin.Default() e.Static("assets" , "./assets" ) e.LoadHTMLGlob("tempates/*" ) e.GET("/login" , gologin) e.POST("/login" , login) e.Run() }
index.html 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" > <meta http-equiv ="X-UA-Compatible" content ="IE=edge" > <meta name ="viewport" content ="width=device-width, initial-scale=1.0" > <title > Document</title > </head > <body > <h1 > 主页</h1 > {{.name}} {{.password}} </body > </html >
login.html 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 29 30 31 32 33 34 35 36 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" > <meta http-equiv ="X-UA-Compatible" content ="IE=edge" > <meta name ="viewport" content ="width=device-width, initial-scale=1.0" > <link rel ="stylesheet" href ="./assets/css/login.css" > <title > login</title > </head > <body > <div class ="form-container" > <div class ="formleft" > <img src ="./assets/img/touxiang.png" > </div > <div class ="formright" > <h1 > 欢迎回来</h1 > <p class ="py-1" > 请先登录</p > <form action ="/login" method ="post" > <label > <p > 用户名</p > <input type ="text" name ="user" class ="user-input" > </label > <label > <p > 密码</p > <input type ="password" name ="password" class ="pw-input" > </label > <button class ="login" type ="submit" > 登录</button > </form > <a href ="./register" > <button class ="qiehuan" type ="submit" > 注册</button > </button > </a > </div > </div > </body > </html >
当有多选框时,使用c.PostFormArray("name")
函数得到参数数组。
Gin获取请求参数 GET请求参数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 package mainimport ( "github.com/gin-gonic/gin" ) func login (c *gin.Context) { key := c.Query("wd" ) c.String(200 , key) } func main () { e := gin.Default() e.GET("/login" , login) e.Run() }
POST请求参数 1 2 3 4 5 func fun (c *gin.Context) { username := c.PostForm("username" ) password := c.DefaultPostForm("password" ) c.String(200 , username) }
路径参数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 package mainimport ( "github.com/gin-gonic/gin" ) func login (c *gin.Context) { s := c.Param("username" ) c.String(200 , s) } func main () { e := gin.Default() e.GET("/login/:username" , login) e.Run() }
Gin数据绑定 POST和GET参数绑定 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 package mainimport "github.com/gin-gonic/gin" type User struct { Uaername string `form:"username"` Password string `form:"password"` } func login (c *gin.Context) { var user User c.ShouldBind(&user) c.String(200 , "User:%s" , user) } func main () { e := gin.Default() e.POST("/login" , login) e.Run() }
路径绑定 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 package mainimport ( "github.com/gin-gonic/gin" ) type User struct { Uaername string `uri:"username"` Password string `uri:"password"` } func login (c *gin.Context) { var user User c.ShouldBindUri(&user) c.String(200 , "User:%s" , user) } func main () { e := gin.Default() e.GET("/login/:username/:password" , login) e.Run() }
Gin访问静态文件和模板文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 package mainimport ( "github.com/gin-gonic/gin" ) func gologin (c *gin.Context) { c.HTML(200 , "login.html" , nil ) } func login (c *gin.Context) { username := c.PostForm("username" ) password := c.PostForm("password" ) c.HTML(200 , "index.html" , gin.H{ "username" : username, "password" : password, }) } func main () { e := gin.Default() e.Static("/assets" , "./assets" ) e.LoadHTMLGlob("tempates/*" ) e.GET("/login" , gologin) e.POST("/login" , login) e.Run() }
e.Static("/assets", "./assets")
读取css和js等静态文件 e.LoadHTMLGlob("tempates/*")
读取html等模板文件
cookie 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package mainimport "github.com/gin-gonic/gin" func login (c *gin.Context) { s, err := c.Cookie("username" ) if err != nil { s = "taweizhong" c.SetCookie("username" , s, 60 *60 , "/" , "localhost" , false , true ) } c.String(200 , "test" ) } func main () { e := gin.Default() e.GET("/login" , login) e.Run() }
session 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 29 30 package mainimport ( "fmt" "github.com/gin-contrib/sessions" "github.com/gin-contrib/sessions/cookie" "github.com/gin-gonic/gin" ) func login (c *gin.Context) { sessions := sessions.Default(c) fmt.Printf("sessions:----------- %v\n" , sessions) S := sessions.Get("name" ) fmt.Printf("S:---------- %v\n" , S) if sessions.Get("hello" ) != "world" { sessions.Set("hello" , "world" ) sessions.Save() } fmt.Printf("sessions:------- %v\n" , sessions) } func main () { e := gin.Default() store := cookie.NewStore([]byte ("1" )) e.Use(sessions.Sessions("name" , store)) e.GET("/login" , login) e.Run() }