安装gin
1、框架文档介绍:go get -u github.com/gin-gonic/gin
2、这时候如果遇到问题资源加载不了,解决方法是使用代理(这块有个 go env 的命令,可以查看当前配置),在cmd中运行
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.io,direct
设置后,重新运行 go get -u github.com/gin-gonic/gin 可以很快速的安装
这里也要设置
响应页面给前端
//创建一个服务
ginServer := gin.Default()
//响应页面给前端
ginServer.GET("/index", func(context *gin.Context) {
//context.JSON()返回json数据
context.HTML(200, "index.html", gin.H{
"msg": "这是go后台传来的",
})
})
//服务器端口
ginServer.Run(":8082")
favicon
ginServer.Use(favicon.New("./1.png"))
Gin RestFul 非常简单
//访问地址,处理我们的请求 Request Response
//接收这个请求有一个返回值,我们通过匿名函数func(){}来返回 context上下文接收请求或响应一些数据
ginServer.GET("/hello", func(context *gin.Context) {
context.JSON(200, gin.H{"msg": "hello,world"})
})
ginServer.POST("/user", func(context *gin.Context) {
context.JSON(200, gin.H{"msg": "hello,world"})
})
ginServer.PUT("/user", func(context *gin.Context) {
})
ginServer.DELETE("/user", func(context *gin.Context) {
})
获取请求参数
```go
//usl?userid=xxx&username=kuangshen
ginServer.GET("/user/info", myHander(), func(context *gin.Context) {
userid := context.Query("userid")
username := context.Query("username")
context.JSON(200, gin.H{
"userid": userid,
"username": username,
})
})
// /user/info/1/kuangshen
ginServer.GET("/user/info/:userid/:username", func(context *gin.Context) {
//取出中间件中的值
usersession := context.MustGet("userSession").(string)
log.Panicln(usersession)
userid := context.Param("userid")
username := context.Param("username")
context.JSON(200, gin.H{
"userid": userid,
"username": username,
})
})
Json序列化
//掌握技术后面的运用,掌握基础知识
//前端给后端传JSON
ginServer.POST("/json", func(context *gin.Context) {
//request body
b, _ := context.GetRawData()
var m map[string]interface{}
//序列化包装成json
_ = json.Unmarshal(b, &m)
context.JSON(200, m)
})
form表单
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>我的一个Go Web 页面</title>
<link rel="stylesheet" href="/static/css/style.css">
<script src="/static/js/common.js"></script>
</head>
<body>
<h1>感谢大家支持狂神</h1>
获取后端的数据为
{{.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>
//支持函数式编程
ginServer.POST("/user/add", func(context *gin.Context) {
username := context.PostForm("username")
password := context.PostForm("password")
context.JSON(200, gin.H{
"msg": "ok",
"username": username,
"password": password,
})
})
重定向
ginServer.GET("/test", func(context *gin.Context) {
context.Redirect(301, "https://www.kuangstudy.com")
})
路由
//路由组
userGroup := ginServer.Group("/user")
{
userGroup.GET("/add")
userGroup.POST("/login")
userGroup.POST("/logout")
}
orderGroup := ginServer.Group("/order")
{
orderGroup.GET("/add")
orderGroup.DELETE("/delete")
}
加载静态资源
//加载静态页面
ginServer.LoadHTMLGlob("templates/*")
ginServer.Static("/static", "./static")
Go中的中间件
// 自定义一个中间件 拦截器
func myHander() gin.HandlerFunc {
return func(context *gin.Context) {
//通过中间件,设置的值,在后续处理只要调用了这个中间件的都可以拿到这里的参数
context.Set("userSession", "userid-1")
context.Next() //放行
context.Abort() //阻止
}
}