下载:
go get -u github.com/gin-gonic/gin
go get -u github.com/thinkerou/favicon
测试代码
代码语言:javascript复制 package main
import "github.com/gin-gonic/gin"
func main() {
//创建一个服务
engine := gin.Default()
//访问地址,处理请求
engine.GET("/hello", func(context *gin.Context) {
context.JSON(200, gin.H{"msg": "hello,web"})
})
//服务器端口
engine.Run(":8848")
}
gin支持restful风格。
restful风格的和之前的例子。
之前:
get /user
post /create_user
put /update_user
delete /delete_user
restful风格:
get /user
post /user
put /user
delete /user
代码语言:javascript复制 package main
import (
"github.com/gin-gonic/gin"
"github.com/thinkerou/favicon"
)
func main() {
//创建一个服务
engine := gin.Default()
//图标
engine.Use(favicon.New("icon.png"))
//访问地址,处理请求
engine.GET("/hello", func(context *gin.Context) {
context.JSON(200, gin.H{"msg": "hello,get"})
})
engine.POST("/hello", func(context *gin.Context) {
context.JSON(200, gin.H{"msg": "hello,post"})
})
//服务器端口
engine.Run(":8848")
}
获取后端的数据
代码语言:javascript复制 package main
import (
"github.com/gin-gonic/gin"
"github.com/thinkerou/favicon"
"net/http"
)
func main() {
//创建一个服务
engine := gin.Default()
//图标
engine.Use(favicon.New("icon.png"))
//加载静态页面
engine.LoadHTMLGlob("templates/*")
//访问地址,处理请求
engine.GET("/index", func(context *gin.Context) {
context.HTML(http.StatusOK, "index.html", gin.H{"msg": "你好啊"})
})
engine.GET("/hello", func(context *gin.Context) {
context.JSON(200, gin.H{"msg": "hello,get"})
})
engine.POST("/hello", func(context *gin.Context) {
context.JSON(200, gin.H{"msg": "hello,post"})
})
//服务器端口
engine.Run(":8848")
}
html页面
代码语言:javascript复制 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>我是静态页面</title>
</head>
<body>
{{.msg}}
</body>
</html>
加载资源文件
static/css/style.css
代码语言:javascript复制 body{
background: red;
}
static/js/common.js
代码语言:javascript复制 alert(123)
templates/index.html
代码语言:javascript复制 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>我是静态页面</title>
<link rel="stylesheet" href="../static/css/style.css">
<script src="../static/js/common.js"></script>
</head>
<body>
{{.msg}}
</body>
</html>
main.go
代码语言:javascript复制 package main
import (
"github.com/gin-gonic/gin"
"github.com/thinkerou/favicon"
"net/http"
)
func main() {
//创建一个服务
engine := gin.Default()
//图标
engine.Use(favicon.New("icon.png"))
//加载静态页面
engine.LoadHTMLGlob("templates/*")
//加载资源文件
engine.Static("/static", "./static")
//访问地址,处理请求
engine.GET("/index", func(context *gin.Context) {
context.HTML(http.StatusOK, "index.html", gin.H{"msg": "你好啊"})
})
engine.GET("/hello", func(context *gin.Context) {
context.JSON(200, gin.H{"msg": "hello,get"})
})
engine.POST("/hello", func(context *gin.Context) {
context.JSON(200, gin.H{"msg": "hello,post"})
})
//服务器端口
engine.Run(":8848")
}
接受参数
代码语言:javascript复制package main
import (
"github.com/gin-gonic/gin"
"github.com/thinkerou/favicon"
"net/http"
)
func main() {
//创建一个服务
engine := gin.Default()
//图标
engine.Use(favicon.New("icon.png"))
//加载静态页面
engine.LoadHTMLGlob("templates/*")
//加载资源文件
engine.Static("/static", "./static")
//接受前端传递过来的参数
//user/info?userid=xxx&username=xxx 传统风格
engine.GET("/user/info", func(context *gin.Context) {
userid := context.Query("userid")
username := context.Query("username")
context.JSON(http.StatusOK, gin.H{
"userid": userid,
"username": username,
})
})
//user/info/xxx/xxx restful风格
engine.GET("/user/info/:userid/:username", func(context *gin.Context) {
userid := context.Param("userid")
username := context.Param("username")
context.JSON(http.StatusOK, gin.H{
"userid": userid,
"username": username,
})
})
//服务器端口
engine.Run(":8848")
}
form表单数据获取
templates/index.html
代码语言:javascript复制<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>我是静态页面</title>
<link rel="stylesheet" href="../static/css/style.css">
<script src="../static/js/common.js"></script>
</head>
<body>
{{.msg}}
<form action="/user/add" method="post">
<p>username:<input type="text" name="username"></p>
<p>password:<input type="text" name="password"></p>
<button type="submit">提交</button>
</form>
</body>
</html>
main.go
代码语言:javascript复制package main
import (
"github.com/gin-gonic/gin"
"github.com/thinkerou/favicon"
"net/http"
)
func main() {
//创建一个服务
engine := gin.Default()
//图标
engine.Use(favicon.New("icon.png"))
//加载静态页面
engine.LoadHTMLGlob("templates/*")
//加载资源文件
engine.Static("/static", "./static")
//访问地址,处理请求
engine.GET("/index", func(context *gin.Context) {
context.HTML(http.StatusOK, "index.html", gin.H{"msg": "你好啊"})
})
//form表单提交
engine.POST("/user/add", func(context *gin.Context) {
username := context.PostForm("username")
password := context.PostForm("password")
context.JSON(http.StatusOK, gin.H{
"msg": "ok",
"username": username,
"password": password,
})
})
//服务器端口
engine.Run(":8848")
}
重定向
代码语言:javascript复制package main
import (
"github.com/gin-gonic/gin"
"github.com/thinkerou/favicon"
"net/http"
)
func main() {
//创建一个服务
engine := gin.Default()
//图标
engine.Use(favicon.New("icon.png"))
//加载静态页面
engine.LoadHTMLGlob("templates/*")
//加载资源文件
engine.Static("/static", "./static")
//重定向
engine.GET("/re", func(context *gin.Context) {
context.Redirect(http.StatusMovedPermanently, "http://www.baidu.com")
})
//服务器端口
engine.Run(":8848")
}
404页面
代码语言:javascript复制package main
import (
"github.com/gin-gonic/gin"
"github.com/thinkerou/favicon"
"net/http"
)
func main() {
//创建一个服务
engine := gin.Default()
//图标
engine.Use(favicon.New("icon.png"))
//加载静态页面
engine.LoadHTMLGlob("templates/*")
//加载资源文件
engine.Static("/static", "./static")
//404页面
engine.NoRoute(func(context *gin.Context) {
context.HTML(http.StatusNotFound, "404.html", nil)
})
//服务器端口
engine.Run(":8848")
}
自定义中间件
代码语言:javascript复制package main
import (
"github.com/gin-gonic/gin"
"github.com/thinkerou/favicon"
"log"
"net/http"
)
func main() {
//创建一个服务
engine := gin.Default()
//图标
engine.Use(favicon.New("icon.png"))
//加载静态页面
engine.LoadHTMLGlob("templates/*")
//加载资源文件
engine.Static("/static", "./static")
//访问地址,处理请求
engine.GET("/index", func(context *gin.Context) {
context.HTML(http.StatusOK, "index.html", gin.H{"msg": "你好啊"})
})
//form表单提交
engine.POST("/user/add", myHandler(), func(context *gin.Context) {
s := context.MustGet("usersession").(string)
log.Println("==============================>", s)
username := context.PostForm("username")
password := context.PostForm("password")
context.JSON(http.StatusOK, gin.H{
"msg": "ok",
"username": username,
"password": password,
})
})
//服务器端口
engine.Run(":8848")
}
func myHandler() gin.HandlerFunc {
return func(context *gin.Context) {
//通过自定义的中间件,设置的值,在后续的处理只要调用了这个中间件的都可以拿到这里的参数
context.Set("usersession", "valueid-01")
context.Next() //放行
//context.Abort()//阻止
}
}