Questions tagged [go-gin]

Gin is a HTTP web framework written in Go.

It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.

Docs

GitHub

755 questions
56
votes
9 answers

Go gin framework CORS

I'm using Go gin framework gin func CORSMiddleware() gin.HandlerFunc { return func(c *gin.Context) { c.Writer.Header().Set("Content-Type", "application/json") c.Writer.Header().Set("Access-Control-Allow-Origin", "*") …
qwertmax
  • 3,120
  • 2
  • 29
  • 42
44
votes
7 answers

How to pass arguments to router handlers in Golang using Gin web framework?

I'm using Gin, https://gin-gonic.github.io/gin/, to build a simple RESTful JSON API with Golang. The routes are setup with something like this: func testRouteHandler(c *gin.Context) { // do smth } func main() { router := gin.Default() …
Niklas9
  • 8,816
  • 8
  • 37
  • 60
42
votes
4 answers

How to get all query parameters from go *gin.context object

I am looking at https://godoc.org/github.com/gin-gonic/gin documentation for a method which returns list of all the query parameters passed. There are methods which return value of a query parameter. Is there any method which returns list of all the…
codec
  • 7,978
  • 26
  • 71
  • 127
35
votes
2 answers

Return an empty array instead of null with golang for json return with gin

So i have a struct : type ProductConstructed struct { Name string `json:"Name"` BrandMedals []string `json:"BRAND_MEDALS"` } When i return my object with gin and : func contructproduct(c *gin.Context) { var response ProductConstructed…
Bussiere
  • 500
  • 13
  • 60
  • 119
34
votes
2 answers

Setting up Route Not Found in Gin

I've setup a default router and some routes in Gin: router := gin.Default() router.POST("/users", save) router.GET("/users",getAll) but how do I handle 404 Route Not Found in Gin? Originally, I was using httprouter which I understand Gin uses so…
tommyd456
  • 10,443
  • 26
  • 89
  • 163
28
votes
4 answers

Make mock gin.Context in Golang

I'm writing a REST API using Gin framework. But I was faced a trouble testing my controllers and researching TDD and Mock. I tried to apply TDD and Mock to my code but I could not. I created a very reduced test environment and tried to create a…
Wilson Tamarozzi
  • 606
  • 1
  • 8
  • 10
21
votes
2 answers

How to log response body in gin

I need to log the response body in a middleware of gin, but I don't find how to get the response body. Can anyone help? I am using a middleware like this: func Logger() gin.HandlerFunc { return func(c *gin.Context) { c.Next() …
John Zeng
  • 1,174
  • 2
  • 9
  • 22
20
votes
4 answers

Go and Gin: Passing around struct for database context?

I've just started trying out Go, and I'm looking to re-implement an API server written in node with it. I've hit a hurdle with trying to use dependency injection to pass around a database context as a gin middleware. So far I've set it up as…
Tane Piper
  • 1,121
  • 2
  • 8
  • 17
18
votes
1 answer

Does gin-gonic process requests in parallel?

We have a API server written in go that is based on gin-gonic. We've noticed something odd that has led us to believe that it is processing requests serially rather than the expected parallel operation. Consider this log file: [GIN] 2016/04/05 -…
mlewis54
  • 2,372
  • 6
  • 36
  • 58
17
votes
2 answers

Simplest way to return literal JSON using gin gonic

I am learning Go by building a simple API interface for a web server. I want to return a simple message in JSON, when a default route is hit. So far, reading online, this is the easiest way to return a literal JSON string, and encode it and send it…
tritium_3
  • 648
  • 1
  • 8
  • 17
16
votes
5 answers

How to set gin mode to release mode?

I need to set gin mode to release mode. How should I do it? Now when I run my API there is a hint like this: [GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production. - using env: export GIN_MODE=release - using…
Soroosh
  • 543
  • 1
  • 4
  • 21
16
votes
2 answers

Gin router: path segment conflicts with existing wildcard

I want to make my app to serve below things. a.com => serve /www to a browser so that the browser can seek /www/index.html) a.com/js/mylib.js => serve /www/js/mylib.js to a browser a.com/api/v1/disk => typical REST API which returns…
Xeph
  • 434
  • 5
  • 10
15
votes
2 answers

Go Gin-Gonic, get text from POST request

I'm starting to develop a REST API using Go and package Gin-Gonic. The idea is to create a REST API that receives POST requests in a JSON format and redirects this call to another application (also a API). Here is a piece of code: package…
Salami
  • 317
  • 1
  • 2
  • 9
14
votes
2 answers

How to handle errors in Gin middleware

I want to grab all http errors on each route without rewrite each time if 400 then if 404 then if 500 then etc... so I have an ErrorHandler() function inside each route handler: func (h *Handler) List(c *gin.Context) { movies, err :=…
John
  • 4,711
  • 9
  • 51
  • 101
14
votes
3 answers

How to install Gin with Golang

I'm a newbie on Golang, and I'm trying to use Gin to develop a web server on Ubuntu 16.04. After executing go get -u github.com/gin-gonic/gin, many folders appear at ~/go/pkg/mod/github.com/. Then I try to make an example: package main import…
Yves
  • 11,597
  • 17
  • 83
  • 180
1
2 3
50 51