Go是一门正在快速增长的编程语言,专为构建简单、快速且可靠的软件而设计。golang提供的nethttp库已经很好了,对于http的协议的实现非常好,基于此再造框架,也不会是难事,因此生态中出现了很多框架。
Gin: Go 语言编写的Web框架,以更好的性能实现类似Martini框架的API。
- Gin是一个golang的微框架,封装比较优雅,API友好,源码注释比较明确。具有快速灵活,容错方便等特点。Beego:开源的高性能Go语言Web框架。
 
 
beego是一个快速开发Go应用的http框架,go语言方面技术大牛。
- beego可以用来快速开发API、Web、后端服务等各种应用,是一个RESTFul的框架,主要设计灵感来源于tornado、sinatra、flask这三个框架,但是结合了Go本身的一些特性(interface、struct继承等)而设计的一个框架。lris:全宇宙最快的Go语言Web框架。完备MVC支持,未来尽在掌握。
 
 
lris是一个快速,简单但功能齐全的和非常有效的web框架。
- 提供了一个优美的表现力和容易使用你的下一个网站或API的基础。
 
 
Gin
安装Gin
Gin是一个golang的微框架,封装比较优雅,API友好,源代码比较明确。具有快速灵活,容错方便等特点。其实对于golang而言,web框架的依赖远比Python,Java之类的要小。自身的net/http足够简单,性能也非常不错。框架更像是一个常用函数或者工具的集合。
借助框架开发不仅可以省去很多常用的封装带来的时间,也有助于团队的编码风格和形成规范。
Gin官方文档地址: https://gin-gonic.com/zh-cn/docs/
安装命令:
go get -u github.com/gin-gonic/gin
   | 
 
HelloWorld
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(":8082") }
   | 
 
目录结构

访问localhost:8082/hello

添加icon
效果

实现代码

package main
  import "github.com/gin-gonic/gin" import "github.com/thinkerou/favicon"
  func main() { 	 	ginServer := gin.Default() 	ginServer.Use(favicon.New("./彩虹.png")) 	 	ginServer.GET("/hello", func(context *gin.Context) { 		context.JSON(200, gin.H{"msg": "hello,world"}) 	}) 	 	ginServer.Run(":8082") }
   | 
 
利用restful形式开发
package main
  import "github.com/gin-gonic/gin" import "github.com/thinkerou/favicon"
  func main() { 	 	ginServer := gin.Default() 	ginServer.Use(favicon.New("./彩虹.png")) 	 	 	ginServer.GET("/hello", func(context *gin.Context) { 		context.JSON(200, gin.H{"msg": "hello,get"}) 	}) 	ginServer.POST("/hello", func(context *gin.Context) { 		context.JSON(200, gin.H{"msg": "hello,post"}) 	}) 	ginServer.DELETE("/hello", func(context *gin.Context) { 		context.JSON(200, gin.H{"msg": "hello,DELETE"}) 	}) 	ginServer.PUT("/hello", func(context *gin.Context) { 		context.JSON(200, gin.H{"msg": "hello,PUT"}) 	}) 	 	 	ginServer.Run(":8082") }
   | 
 
测试都是成功的

返回页面
返回了页面并且后端向前端传输了数据
目录结构:

代码实现:
index.html
<!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <title>这是Gin的页面</title> </head> <body>     <h1>hello Gin</h1>
          {{.msg}} </body> </html>
   | 
 
main.go
package main
  import ( 	"github.com/gin-gonic/gin" 	"net/http" ) import "github.com/thinkerou/favicon"
  func main() { 	 	ginServer := gin.Default() 	ginServer.Use(favicon.New("./彩虹.png")) 	 	 	ginServer.LoadHTMLGlob("templates/*") 	 	 	ginServer.GET("/index", func(context *gin.Context) { 		 		 		 		context.HTML(http.StatusOK, "index.html", gin.H{"msg": "这是后端传送的数据"}) 	}) 	 	ginServer.Run(":8082") }
   | 
 

引入样式和脚本文件
目录结构

代码实现
index.html

main.go

main.css

index.js

前后端传参
前端通过url方式传参给后端
前端通过url方式传参并且后端接收到参数回显到前端
代码实现:

运行效果:

前端通过restful方式传参给后端
代码实现:

运行效果:

前端通过发送json的方式传参给后端
接收代码

传参:

注意这里使用了我们之前写的index接口,才可以访问到index.html页面的
代码实现:
后端

前端

实际效果:


路由
重定向
后端代码实现:
 	ginServer.GET("/test", func(context *gin.Context) { 		 		          		context.Redirect(http.StatusMovedPermanently, "https://xiaobaicai350.github.io/") 	})
 
  | 
 

重定向到了我的博客

404
目录结构

main.go

404.html

访问一个随便的后缀,后端会帮我们拦截到,然后转发到404页面

路由组
说白了就是加个前缀
 userGroup := ginServer.Group("/user") { 	 	userGroup.GET("/add", func(context *gin.Context) { 		context.HTML() 	}) 	 	userGroup.GET("/logout") 	 	userGroup.GET("/login") }
 
  | 
 
中间件
就是Java里面的拦截器
定义我们自己的拦截器:

使用拦截器:

验证结果:
访问http://localhost:8082/testHandler
