草庐IT

gin_trgm_ops

全部标签

forms - 如何为具有 gin(框架)和 golang 接口(interface)的表单制作通用表单函数?

我想创建一个函数来处理任何类型的表单。我希望它能够处理任何类型的数据。我正在尝试使用界面来完成这项任务。typePersonstruct{namestringlastNamestring}funcHTMLForm(c*gin.Context){varfPersonc.ShouldBind(&f)c.JSON(http.StatusOK,f)}//withthisfunctionigetthecorrectinfo//output:{"name":"john","lastName":"snow"}funcHTMLForm(c*gin.Context){varfinterface{}c.S

戈朗 : Mixing Gin with an UDP server

我正在尝试同时使用UDP服务器连续监听数据报和http服务器,但是字符串“UDP服务器启动并监听端口...”和命令“server.Run()”从未达到.packagemainimport("fmt""github.com/gin-gonic/gin""log""net")funchandleUDPConnection(conn*net.UDPConn){buffer:=make([]byte,8096)n,addr,err:=conn.ReadFromUDP(buffer)iferr!=nil{log.Fatal(err)}else{fmt.Println("UDPclient:",a

go - 为什么 post 请求在 gin golang 中不起作用?

这段代码不起作用,响应将是空的,就像这样{"test":""}。funcmain(){router:=gin.Default()router.POST("/test",func(c*gin.Context){test:=c.Query("test")c.JSON(200,gin.H{"test":test,})})router.Run()}更新:我通过结构找到了简单的解决方案:functest(c*gin.Context){test:=struct{Teststring`json:"test"`Test2string`json:"test2"`}{}c.BindJSON(&test)c

json - 如何在 go gin 中获取从 JSON 发布的文件?

我想保存由JSON发布的图像文件。这是帖子的结构:typeArticlestruct{Titlestring`json:"title"`Bodystring`json:"body"`File[]byte`json:"file"`}处理程序是:funcPostHandler(c*gin.Context){varerrerrorvarjsonArticleerr=c.BindJSON(&json)iferr!=nil{log.Panic(err)}//handlephotouploadvarfilenamestringfile,header,err:=json.File//assignme

go - 有没有办法关闭 golang/gin 中的客户端请求?

使用gin框架。有没有办法通知客户端关闭请求连接,然后服务器处理程序可以在不让客户端等待连接的情况下执行任何后台作业?funcTest(c*gin.Context){c.String(200,"ok")//closeclientrequest,thendosomejobs,forexamplesyncdatawithremoteserver.//} 最佳答案 是的,你可以做到。通过简单地从处理程序返回。而你想做的后台工作,你应该把它放在一个新的goroutine上。请注意,连接和/或请求可能会放回池中,但这无关紧要,客户端将看到服务

session - 在 golang 中将 map、struct 设置为 session (gin gonic 框架)

我正在使用gingonic构建网络应用程序。我用https://github.com/gin-gonic/contrib/tree/master/sessions处理session。例如,我为session设置了一个整数值:functionTest(c*gin.Context){session:=sessions.Default(c)session.Set("mysession",123)session.Save()}在另一个Controller中,我可以通过session.Get("mysession")获取这个session。但是如果我设置map或struct。我只能在同一个Con

rest - gin-gonic 无法分配请求的地址

所以我目前正在使用gin-gonic包在go中构建一个restfulapi。我希望将代码部署到谷歌云平台计算引擎VM。当我在我的本地机器上运行代码时,它使用本地主机工作,但是当在指定外部IP的实际VM实例上运行它时,我收到TCP连接的绑定(bind)错误。任何帮助表示赞赏。server.gopackagemainimport("encoding/json""io/ioutil""net/http""os""github.com/gin-gonic/gin")typeheadlinesstruct{AuthorstringTitlestringDescriptionstringUrlst

Golang gin gonic web 框架代理路由到另一个后端

如何在GinGonic中将一些路由的代理Web请求反向到另一个后端网页golang框架有没有办法在Handle函数中直接转发如下图?路由器:=gin.New()router.Handle("POST","/api/v1/endpoint1",ForwardToAnotherBackend) 最佳答案 这是我用于将特定端点子集从gin框架反向代理到另一个后端的解决方案:router.POST("/api/v1/endpoint1",ReverseProxy())和:funcReverseProxy()gin.HandlerFunc{t

go - 如何定义一个中间有 id 的 go-gin 路由

我要定义一条路线/user/{userid}/status如何定义这种路由并在处理程序中拦截用户标识。像这样r.GET("/user/{userid}/status",userStatus)在这种情况下如何读取我的Go代码中的userid变量? 最佳答案 您可以使用userid:=c.Param("userid"),就像这个工作示例:packagemainimport("fmt""net/http""github.com/gin-gonic/gin")funcmain(){router:=gin.Default()router.GE

go - 如何使用 golang gin 为 restful API 创建身份验证模型?

我希望为我的restfulAPI创建一个身份验证模型。希望使用APItoken,我在Web服务中使用MVC,我创建了一个像这样的auth.goController。packagecontrollersimport("github.com/gin-gonic/gin""os"//"github.com/jinzhu/gorm")typeAdsControllerAuthstruct{}func(ac*AdsControllerAuth)TokenAuthMiddlewaregin.HandlerFunc{returnfunc(c*gin.Context){token:=c.Request