Gin学习

博客围绕Gin这个web框架展开学习介绍。内容包括获取Gin、导入Gin、配置环境,实现第一个hello world程序,设置favicon,使用前端页面模板,接收get和POST参数,处理路由重定向、404情况及路由组,还详细阐述了中间件的定义、注册和使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Gin是一个web框架

获取Gin

提前设置个全球代理

go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.io,direct
go get -u github.com/gin-goinc/gin

导入gin

IDEA中环境配置一下
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7b2BwJiH-1674708083899)(../images/Pasted%20image%2020230124114506.png)]

第一个hello world

package main  
  
import "github.com/gin-gonic/gin"  
  
func main() {  
   ginServer := gin.Default()  
  
   ginServer.GET("/hello", func(context *gin.Context) {  
      context.JSON(200, gin.H{"msg": "hello world"})  
   })  
  
   ginServer.Run(":8899")  
}

设置favicon

ginServer.Use(favicon.New(“./favicon.ico”))

前端页面模板

package main  
  
import "github.com/gin-gonic/gin"  
import "github.com/thinkerou/favicon"  
  
func main() {  
   ginServer := gin.Default()  
   ginServer.Use(favicon.New("./favicon.ico"))  
  
   //加载静态页面  
   ginServer.LoadHTMLGlob("tmplate/*")  
   //ginServer.LoadHTMLFiles("tmplate/*")  
  
   ginServer.GET("/hello", func(context *gin.Context) {  
      context.JSON(200, gin.H{"msg": "hello world"})  
   })  
  
   ginServer.POST("/post", func(context *gin.Context) {  
      context.JSON(200, gin.H{"msg": "post"})  
   })  
  
   //响应一个页面  
   ginServer.GET("/", func(context *gin.Context) {  
      context.HTML(200, "index.html", gin.H{  
         "msg": "动态数据传输",  
      })  
   })  
  
   ginServer.Run(":8899")  
}
<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <title>yes</title>  
</head>  
<body>  
<h1>hello boys</h1>  
{{.msg}}  
</body>  
</html>

接收参数

get

ginServer.GET("/info", func(context *gin.Context) {  
   username := context.Query("username")  
   context.JSON(http.StatusOK, gin.H{  
      "username": username,  
   })  
})

POST

ginServer.POST("/login", func(context *gin.Context) {  
   username := context.PostForm("username")  
   password := context.PostForm("password")  
   context.String(200, username)  
   context.String(200, password)  
   context.String(200, "welcome"+username)  
})

路由

重定向

ginServer.GET("/redirect", func(context *gin.Context) {  
   context.Redirect(301, "http://127.0.0.1/login")  
})

404

ginServer.NoRoute(func(context *gin.Context) {  
   context.HTML(404, "404.html", nil)  
})

路由组

//路由组  
userGroup := ginServer.Group("/user")  
{  
   userGroup.GET("/info")  
   userGroup.GET("/login")  
}

中间件

定义中间件

//自定义一个中间件  
func Handler() gin.HandlerFunc {  
   return func(context *gin.Context) {  
      // 自动中间件,设置的值,在后续处理只要调用了中间件都可以拿到这里的参数  
      context.Set("usersession", "userid-1")  
      if true{  
         context.Next()//放行  
      }  
      context.Abort()//阻止  
      }  
}

注册中间件

//注册中间件  
ginServer.Use(Handler())

使用中间件

ginServer.GET("/hello",Handler(), func(context *gin.Context) {  
   context.JSON(200, gin.H{"msg": "hello world"})  
})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值