快速上手GoWeb开发之Gin框架

var code = “f559facb-f001-463f-97ab-1a9884f3dada”
Go是一门正在快速增长的编程语言,专为构建简单、快速且可靠的软件而设计。 golang提供的net/htp库已经很好了,对于htp的协议的实现非常好,基于此再造框架,也不会是难事,因此生态中出现了很多框架。

Gin: Go 语言编写的 Web 框架,以更好的性能实现类似 Martini 框架的 API.

Gin是一个golang的微框架,封装比较优雅,API友好,源码注释比较明确。具有快速灵活,容错方便等特点。Beego: 开源的高性能 Go 语言 Web 框架

beego是一个快速开发Go应用的htp框架。

go 语言方面技术大牛beego可以用来快速开发AP1、Web、后端服务等各种应用,是一个RESTFUul的框架,主要设计灵感来源于tornado、sinatra flask这三个框架,但是结合了Go本身的一些特性(interfae、strut继承等)而设计的一个框架。

Iris: 全宇宙最快的 Go 语言 Web 框架。完备 MVC 支持,未来尽在掌握。

lris是一个快速,简单但功能齐全的和非常有效的web框架。提供了一个优美的表现力和容易使用你的下一个网站或API的基础。

Gin安装使用

Gin是一个golang的微框架,封装比较优雅,API友好,源代码比较确。具有快速灵活,容方便等特点。其实对于golang而言,web框架的依赖远比Python,Java之类的要小。自身的net/http足够简单,性能也非常不错。框架更像是一个常用函数或者工具的集合。借助框架开发,不仅可以省去很多常用的封装带来的时间,也有助于团队的编码风格和形成规范。
Gin 官方文档地址: https://gin-gonic.com/zh-cn/docs/

安装gin
 go get -u github.com/gin-gonic/gin
处理下载github包报错问题

问题描述:由于网络原因访问github网站会异常缓慢。

解决办法:配置国内镜像。

具体配置:

1、修改环境变量,增加以下配置:

GO111MODULE=on
GOPROXY=https://goproxy.io

快速上手GoWeb开发之Gin框架_第1张图片

2、修改GoLand配置:

打开设置,找到Go Modules,增加配置

GOPROXY=https://goproxy.io

快速上手GoWeb开发之Gin框架_第2张图片

RestFul Api

旧版请求:
get /user
post /create_user
post /update_user
post /delete_user
Restful Api:
get /user
post /user
put /user
delete /user
加载静态页面
ginServer := gin.Default()
ginServer.Use(favicon.New("./微信图片_20220629135324.ico"))
//加载静态页面
ginServer.LoadHTMLGlob("templates/*")
//加载资源文件
ginServer.Static("/static", "./static")
接收前端传递的参数,地址中包含参数
///user/info?userid=1&username=wk
ginServer.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/1/wk
ginServer.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,
   })
})
前端给后端传递json
ginServer.POST("/json", func(context *gin.Context) {

   //返回[]byte
   data, _ := context.GetRawData()
   var m map[string]interface{}

   //包装为json数据 []byte
   _ = json.Unmarshal(data, &m)
   context.JSON(http.StatusOK, m)
})
支持函数式编程
ginServer.POST("/user/add", func(context *gin.Context) {
   username := context.PostForm("username")
   password := context.PostForm("password")
   context.JSON(http.StatusOK, gin.H{
      "mag":      "ok",
      "username": username,
      "password": password,
   })
   //context.HTML(http.StatusOK, "login.html", gin.H{
   // "msg": "这是go后台传递过来的数据",
   //})
})
路由
ginServer.GET("/test", func(context *gin.Context) {
   //重定向 301
   context.Redirect(301, "https://www.baidu.com")
})

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

//路由组
userGroup := ginServer.Group("/user")
{
   userGroup.GET("/add")
}
自定义Go中间件 拦截器
//定义
func myHandler() gin.HandlerFunc {
   return func(context *gin.Context) {
      //通过自定义中间件,设置的值,在后续的处理只要调用这个中间件都可以拿到这里的参数
      context.Set("userSession", "userId-1")
      //放行
      context.Next()
      //阻止
      //context.Abort()
   }
}

//引入
ginServer.Use(myHandler())

//使用
ginServer.GET("/user/info", myHandler(), func(context *gin.Context) {

		userSession := context.MustGet("userSession")
		log.Println(userSession)

		userId := context.Query("userid")
		userName := context.Query("username")
		context.JSON(http.StatusOK, gin.H{
			"userId":   userId,
			"userName": userName,
		})
	})

你可能感兴趣的:(Go语言,gin,golang)