16

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 code:  gin.SetMode(gin.ReleaseMode)

I tried gin.SetMode(gin.ReleaseMode) but it does not work. I initialize my router here:

gin.SetMode(gin.releaseMode)    
router := gin.Default()
Soroosh
  • 543
  • 1
  • 4
  • 21

5 Answers5

39

You have to call SetMode method before you initialize the gin router. E.x:

gin.SetMode(gin.ReleaseMode)
router := gin.New()
NameError
  • 536
  • 4
  • 3
25

Just set GIN_MODE=release to your environment config.

Kunal Kapadia
  • 3,223
  • 2
  • 28
  • 36
4

Just add gin.SetMode(gin.ReleaseMode) in your main function.

Molly1337
  • 73
  • 6
4
gin.SetMode(gin.ReleaseMode)

This works. Remember that you need to set this before creating the router in your init function/main. It does not seem to work otherwise i.e Your code would look something like this.

func init() {
    gin.SetMode(gin.ReleaseMode)
    r := NewRouter()
    err := r.Run("8080")
}

The init function is called for each source file after all the packages are imported and the variables are initialized. https://golang.org/doc/effective_go.html#init

Community
  • 1
  • 1
The 0bserver
  • 826
  • 9
  • 18
-1

It would seem you do this by calling the SetMode method from within your app. Probably in your main, or possibly in an init function.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189