起步
go mod init quikestart
go get -u github.com/gin-gonic/gin
main.go
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
router := gin.Default()
router.GET("", func(c *gin.Context) {
c.String(http.StatusOK, "Hello World")
})
router.Run(":8080")
}
如何返回 HTML 文件
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func home(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", gin.H{})
}
func main() {
router := gin.Default()
// 指定模板目录
router.LoadHTMLGlob("templates/*")
router.GET("/", home)
router.Run(":8080")
}
如何返回 JSON
package main
import (
"github.com/gin-gonic/gin"
)
func home(c *gin.Context) {
c.JSON(200, gin.H{
"message": "hello world",
"data": []string{"西游记", "国色天香"},
})
}
func main() {
router := gin.Default()
// 指定模板目录
router.LoadHTMLGlob("templates/*")
router.GET("/", home)
router.Run(":8080")
}
接受 POST 数据 / 登录案例
Content-Type: application/x-www-form-urlencoded
main.go
package main
import (
"github.com/gin-gonic/gin"
)
func home(c *gin.Context) {
c.JSON(200, gin.H{
"message": "hello world",
"data": []string{"西游记", "国色天香"},
})
}
func login(c *gin.Context) {
c.HTML(200, "login.html", nil)
}
func auth(c *gin.Context) {
username := c.PostForm("username")
password := c.PostForm("password")
if username == "admin" && password == "123456" {
c.JSON(200, gin.H{
"message": "登录成功",
})
} else {
c.JSON(200, gin.H{
"message": "用户名或密码错误",
})
}
/* c.JSON(200, gin.H{
"username": username,
"password": password,
}) */
}
func main() {
router := gin.Default()
// 指定模板目录
router.LoadHTMLGlob("templates/*")
router.GET("/login", login)
router.POST("/auth", auth)
router.GET("/", home)
router.Run(":8080")
}
templates/login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/auth" method="post">
<label for="username">用户名</label>
<input type="text" name="username" id="username"><br>
<label for="password">密码:</label>
<input type="text" name="password" id="password"><br>
<input type="submit">
</form>
</body>
</html>
后端渲染模板和返回变量
package main
import (
"github.com/gin-gonic/gin"
"time"
)
func timer(c *gin.Context) {
now := time.Now().Unix()
c.HTML(200, "timer.html", gin.H{
"now": now,
})
}
func main() {
router := gin.Default()
// 指定模板目录
router.LoadHTMLGlob("templates/*")
router.GET("/timer", timer)
router.Run(":8080")
}
templates/timer.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h3>当前时间:{{.now}}</h3>
</body>
</html>
RESTFul 风格路由
package main
import (
"github.com/gin-gonic/gin"
)
func getUser(c *gin.Context) {
c.JSON(200, gin.H{
"message": "hello world",
})
}
func addUser(c *gin.Context) {
}
func getOneUser(c *gin.Context) {
}
func delOneUser(c *gin.Context) {
}
func putOneUser(c *gin.Context) {}
func main() {
router := gin.Default()
// 指定模板目录
router.LoadHTMLGlob("templates/*")
router.POST("/user", addUser)
router.DELETE("/user/:id", delOneUser)
router.PUT("/user/:id", putOneUser)
router.GET("/user", getUser)
router.GET("/user/:id", getOneUser)
// 无论什么请求,只要路径匹配就会命中
router.Any("/xxx", func(context *gin.Context) {
})
// 上面都匹配失败会走这儿
router.NoRoute(func(c *gin.Context) {})
router.Run(":8080")
}
路由分组
package main
import (
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
bookRouter := router.Group("/book")
{
bookRouter.GET("/", func(context *gin.Context) {})
bookRouter.POST("/add", func(context *gin.Context) {})
}
publishRoute := router.Group("/publish")
{
publishRoute.GET("/", func(context *gin.Context) {})
publishRoute.POST("/add", func(context *gin.Context) {})
}
router.Run(":8080")
}