前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >gin简单学习,自己根据狂神课程写的笔记。

gin简单学习,自己根据狂神课程写的笔记。

原创
作者头像
用户8478947
发布2023-01-25 19:01:20
4360
发布2023-01-25 19:01:20
举报
文章被收录于专栏:安全学习安全学习

下载:

  • 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()//阻止
   }
}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 测试代码
  • 获取后端的数据
  • 加载资源文件
  • 接受参数
  • form表单数据获取
  • 重定向
  • 404页面
  • 自定义中间件
相关产品与服务
消息队列 TDMQ
消息队列 TDMQ (Tencent Distributed Message Queue)是腾讯基于 Apache Pulsar 自研的一个云原生消息中间件系列,其中包含兼容Pulsar、RabbitMQ、RocketMQ 等协议的消息队列子产品,得益于其底层计算与存储分离的架构,TDMQ 具备良好的弹性伸缩以及故障恢复能力。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档