go-gin

How get DATA from frontend in gin?

南楼画角 提交于 2020-06-29 05:26:18
问题 To my shame, I have not been able to figure out how to get data from the frontend in Gin framework. In Django I get data So: user=request.data.get('user') print(user) Everything is simple and understandable as day. How should I do it in gin? user := c.Query("user") user := c.Param("user") user := c.Params.ByName("user") user := c.PostForm("user") println(user)//emptiness.... 回答1: Well, I'd say you should fetch some book/HOWTO on how HTTP work and spend some time with it because it appears you

Is there anyway to close client request in golang/gin?

为君一笑 提交于 2020-01-05 17:57:09
问题 Using gin framework. Is there anyway to notify client to close request connection, then server handler can do any back-ground jobs without letting the clients to wait on the connection? func Test(c *gin.Context) { c.String(200, "ok") // close client request, then do some jobs, for example sync data with remote server. // } 回答1: Yes, you can do that. By simply returning from the handler. And the background job you want to do, you should put that on a new goroutine. Note that the connection and

using := gives unused error but using = don't in Go

一世执手 提交于 2020-01-03 06:01:06
问题 I have piece of code in which I get error when I use := but when I use = it compiles properly. What I learned is that := only requires only atleast one variable to be defined, others need not be defined, but considering this code is it a bug in Go? Uncompilable code: Error: services/db_service.go:16: Session declared and not used package services import ( "gopkg.in/mgo.v2" "log" ) const DB = "mmdb_dev" var Session *mgo.Session func InitMongo() bool { url := "mongodb://localhost" log.Println(

Golang Gin “c.Param undefined (type *gin.Context has no field or method Param)”

廉价感情. 提交于 2019-12-24 00:42:58
问题 I tried to use the Gin which is framework for Golang. https://github.com/gin-gonic/gin And the I copied sample codes from official github. It's like this. package main import ( "github.com/gin-gonic/gin" "net/http" ) func main() { router := gin.Default() router.GET("/user/:name", func(c *gin.Context) { name := c.Param("name") c.String(http.StatusOK, "Hello %s", name) }) router.Run(":8080") } But I got the error. # go run main.go # command-line-arguments ./main.go:12: c.Param undefined (type

Gin - Go lang How to use Context.Request.Body and retain it?

纵然是瞬间 提交于 2019-12-23 17:10:00
问题 I am trying to write a middleware where I will be doing a json schema validation against the request body. After the validation, I need to use the request body again. But I am not able to figure out how this can be done. I referred this post and found a way to access the body. But once the request body is used, I need that available to my next function. Here is the sample code: package main import ( "fmt" "io/ioutil" "net/http" "github.com/gin-gonic/gin" //"github.com/xeipuuv/gojsonschema" )