毛毛是一只狗
经历的意义在于引导你,而非定义你!
展开
-
【Gin⭐015】Go语言Gin框架-优雅退出程序(最后一篇)
当程序退出,我们应该处理一下后续工作,比如增加日志,或者检查没有完成的任务。做相应的处理,然后妥善的退出程序,而不是暴力退出!package mainimport( "fmt" "net/http" "os" "os/signal" "syscall" "time" "github.com/gin-gonic/gin")func main(){ router := gin.Default() router.GET("/test", func(c *gin.Context){原创 2021-09-08 08:42:45 · 738 阅读 · 2 评论 -
【Gin⭐014】Go语言Gin框架-自定义中间件
1:自定义中间件函数2:用Use方法注册中间件函数package mainimport ( "fmt" "github.com/gin-gonic/gin" "net/http" "time")func MyLogger() gin.HandlerFunc { return func(c *gin.Context) { t := time.Now() c.Set("example", "123456") //让原本改执行的逻辑继续执行 c.Next() end原创 2021-09-08 08:41:30 · 134 阅读 · 0 评论 -
【Gin⭐013】Go语言Gin框架-优化错误信息细节
1: 重写处理错误信息里的字段函数2: 格式化错误信息的格式package mainimport ( "fmt" "net/http" "reflect" "strings" "github.com/gin-gonic/gin" "github.com/gin-gonic/gin/binding" "github.com/go-playground/locales/en" "github.com/go-playground/locales/zh" ut "github.com/g原创 2021-09-08 08:40:33 · 173 阅读 · 0 评论 -
【Gin⭐012】Go语言Gin框架-错误信息翻译成中文
1:翻译的通用函数2:初始化通用函数3:转换错误信息格式4:翻译错误信息package mainimport ( "fmt" "net/http" "reflect" "strings" "github.com/gin-gonic/gin" "github.com/gin-gonic/gin/binding" "github.com/go-playground/locales/en" "github.com/go-playground/locales/zh" ut "gith原创 2021-09-08 08:38:58 · 453 阅读 · 0 评论 -
【Gin⭐011】Go语言Gin框架-表单验证-第一步
1:制定请求参数标准2:注册参数标准的结构体3:判断package mainimport ( "fmt" "net/http" "github.com/gin-gonic/gin")//1: 指定验证规则,参数的标准type LoginForm struct { // 前端如果用json传参放到"user"里面, required表示必传的参数 User string `json:"user" binding:"required"` Password string `json:原创 2021-09-08 08:37:36 · 336 阅读 · 0 评论 -
【Gin⭐010】Go语言Gin框架-返回protobuf
下面是gin返回protobuf对象,在client端获取出来后,用protobuf在解析出来的过程。go语言文件syntax = "proto3";option go_package = "./;proto";message Teacher{ string name = 1; repeated string course = 2;}package mainimport ("net/http""github.com/gin-gonic/gin""awesomePro/原创 2021-09-07 20:42:07 · 5480 阅读 · 4 评论 -
【Gin⭐009】Go语言Gin框架-返回struct
package mainimport( "net/http" "github.com/gin-gonic/gin")func main() { g := gin.Default() g.GET("/s", ReStruct) g.Run(":9004")}type Avengers struct{ // tag 在json序列化的时候,会把Name转换成user Name string `json:"user"` Skill string}func ReStruc原创 2021-09-07 20:39:37 · 569 阅读 · 0 评论 -
【Gin⭐008】Go语言Gin框架-get and post一起获取参数
package mainimport( "net/http" "github.com/gin-gonic/gin")func GetPost(c *gin.Context) { name := c.Query("name") age := c.DefaultQuery("age", "19") skill := c.PostForm("skill") corporation := c.DefaultPostForm("corporation", "google") c.JSON(原创 2021-09-07 20:37:58 · 369 阅读 · 0 评论 -
【Gin⭐007】Go语言Gin框架-获取post 表单请求参数
package mainimport( "net/http" "github.com/gin-gonic/gin")func uery(c *gin.Context){ name := c.DefaultQuery("name", "changyu") age := c.DefaultQuery("age", "18") c.JSON(http.StatusOK, gin.H{ "name":name, "age":age, })}func formpost(c *gi原创 2021-09-07 20:37:09 · 595 阅读 · 0 评论 -
【Gin⭐006】Go语言Gin框架-查询字符串传参方式
package mainimport( "net/http" "github.com/gin-gonic/gin")func uery(c *gin.Context){ // 第二个参数是默认值,如果不用默认值,就把DefaultQuery改成Query name := c.DefaultQuery("name", "changyu") age := c.DefaultQuery("age", "18") c.JSON(http.StatusOK, gin.H{ "name原创 2021-09-07 20:36:08 · 189 阅读 · 0 评论 -
【Gin⭐005】Go语言Gin框架-路径传参并验证参数类型
package mainimport ( "github.com/gin-gonic/gin")func main(){ r := gin.Default() r.GET("/user/:id/:name", func(c *gin.Context){ //我把结构体,参数标准放到了handler里面 //初期这样写便于理解 type Request struct{ Id int `uri:"id", binding:"requir原创 2021-09-07 20:32:21 · 285 阅读 · 0 评论 -
【Gin⭐004】Go语言Gin框架-URL分组设置
package mainimport( "net/http" "github.com/gin-gonic/gin")func Pong(c *gin.Context){ c.JSON(http.StatusOK, gin.H{ "Said":"Hello Book", })}func Run(c *gin.Context){ c.JSON(http.StatusOK, gin.H{ "Doing":"Running", })}func main(){ //实原创 2021-09-07 20:30:37 · 176 阅读 · 0 评论 -
【Gin⭐003】Go语言Gin框架-URL设置
package mainimport( "net/http" "github.com/gin-gonic/gin")func Pong(c *gin.Context){ c.JSON(http.StatusOK, gin.H{ "Said":"Hello Book", })}func Run(c *gin.Context){ c.JSON(http.StatusOK, gin.H{ "Doing":"Running", })}func main(){ //实原创 2021-09-07 20:29:47 · 335 阅读 · 0 评论 -
【Gin⭐002】Go语言Gin框架-Default and New初始化Gin的区别
New会舍去logger和recovery两个中间件当用Default初始化当用New初始化原创 2021-09-07 20:19:39 · 634 阅读 · 1 评论 -
【Gin⭐001】Gin初识,hello world
Gin,一个小而轻,go语言实现的web框架,相当于python里的flask框架。他们的共同点都是小而轻,大而全的框架是beego,相当于python的django框架。官方地址:https://github.com/gin-gonic/gingin-hello worldpackage mainimport( "net/http" "github.com/gin-gonic/gin")func Pong(c *gin.Context){ c.JSON(http原创 2021-09-07 20:09:29 · 175 阅读 · 1 评论