Go语言中的Gin框架的初步使用

安装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() //阻止
	}
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值