• 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
    
    • 1
    • 2

    设置后,重新运行 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")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    favicon

    ginServer.Use(favicon.New("./1.png"))
    
    • 1

    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) {
    
    	})
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    在这里插入图片描述

    获取请求参数

    ```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,
    		})
    
    	})
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    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)
    
    	})
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在这里插入图片描述

    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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    //支持函数式编程
    	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,
    		})
    	})
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    重定向

    ginServer.GET("/test", func(context *gin.Context) {
    		context.Redirect(301, "https://www.kuangstudy.com")
    	})
    
    • 1
    • 2
    • 3

    路由

    //路由组
    	userGroup := ginServer.Group("/user")
    	{
    		userGroup.GET("/add")
    		userGroup.POST("/login")
    		userGroup.POST("/logout")
    	}
    	orderGroup := ginServer.Group("/order")
    	{
    		orderGroup.GET("/add")
    		orderGroup.DELETE("/delete")
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    加载静态资源

    //加载静态页面
    	ginServer.LoadHTMLGlob("templates/*")
    	ginServer.Static("/static", "./static")
    
    • 1
    • 2
    • 3

    Go中的中间件

    // 自定义一个中间件 拦截器
    func myHander() gin.HandlerFunc {
    	return func(context *gin.Context) {
    		//通过中间件,设置的值,在后续处理只要调用了这个中间件的都可以拿到这里的参数
    		context.Set("userSession", "userid-1")
    		context.Next()  //放行
    		context.Abort() //阻止
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • 相关阅读:
    云原生架构之服务发现与注册-总述
    froeach迭代删除和List迭代删除问题
    python绘制R控制图(Range Chart)
    React 知识点:基础语法、组件、React-Router、Redux
    1. 封装自己的脚手架 2.创建代码模块
    如何在快节奏的生活下摆脱焦虑?
    1 操作系统任务调度问题----来源刘H同学
    最受欢迎的程序员副业排行榜TOP6
    RabbitMQ实现秒杀场景示例
    金仓数据库KingbaseES GIN 索引
  • 原文地址:https://blog.csdn.net/ZN175623/article/details/127708752