Table of contents
1.
Introduction
2.
What is Go?
3.
What is Gin in Golang?
4.
What is Gin SetMode?
5.
Examples of Gin SetMode
5.1.
Example 1
5.2.
Example 2
5.3.
Example 3
6.
Different modes in Gin
7.
Frequently Asked Questions
7.1.
What is Go?
7.2.
What is Gin?
7.3.
What is Gin SetMode?
7.4.
How to set gin mode to release mode?
7.5.
How to set gin mode to debug mode?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

Gin SetMode

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hey Ninjas, you all would have read about a trendy procedural programming language developed in Google. It is Go; it was developed by Robert Griesemer, Rob Pike, and Ken Thompson in 2007 in Google. To begin with, we will discuss some basics of this language. For more, you can visit Introduction to Go

Introduction

In this article, we will focus on the Gin SetMode, but before that, we will read some basic facts about Go; then, we will learn what gin is in golang. 

What is Go?

Go

Go, or Golang is a popular procedural programming language developed by Google. It was developed in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson. It is fast and can be used to build high-performance applications, making it very popular nowadays. It is as simple as Python and as fast as C/C++. A combination of these two factors is the main reason for its popularity among developers and programmers. While the compilation of Golang code, the code is compiled into binary form, which can be executed on any platform or server of your choice, making it platform-independent. You can also get the whole Go language library here.

What is Gin in Golang?

Gin in Golang

Gin is an HTTP web framework written in the Go programming language and which is high-performance in nature. We use a web framework to simplify our code through the use of some standardized codes during development. So, we use Gin when we need productivity along with very high performance. When compared to the usual Martini-type API, Gin is almost 40 times faster. 

What is Gin SetMode?

What is GIn SetMode

SetMode is a function we use to set the gin mode according to the input string. This input string is taken while writing the SetMode function. This function is found in the Gin package of the Go programming language. Let us see the syntax for the Gin SetMode.

func SetMode(value string)

Here, the string is the input string according to which the compiler will set the gin mode.

Examples of Gin SetMode

Now, we will see some examples to understand Gin SetMode better.

Example 1

We will start with an elementary example. In this example, we are setting the gin mode according to the environment. If the environment is “release,” we set the release mode using gin.SetMode(gin.ReleaseMode). If the environment is “debug,” we set up debug mode using gin.SetMode(gin.DebugMode).

func init() {
	readConfig()
	if config.Environment.Env == "release" {
		gin.SetMode(gin.ReleaseMode)
	}
	if config.Environment.Env == "debug" {
		gin.SetMode(gin.DebugMode)
	}
	client = elasticsearch.NewClientFromUrl(config.Es_cluster.Addr)
}

Example 2

In this example, we are defining a function for the utilities of the gin SetMode. This only is the real definition of the SetMode function. We are achieving this by the use of switch-case statements. The function's parameter is taken as the switch, and then different cases are written based on different values of the switch(that is, mode). If the mode is “release,” then gin.SetMode(gin.ReleaseMode). If the mode is “debug,” gin.SetMode(gin.DebugMode). Suppose the mode is “test” gin.SetMode(gin.TestMode). If the mode is other than any of the three, the function will give panic("mode unavailable. (debug, release, test)").

func SetMode(mode string) {
	switch mode {
	case "release":
		gin.SetMode(gin.ReleaseMode)
	case "debug":
		gin.SetMode(gin.DebugMode)
	case "test":
		gin.SetMode(gin.TestMode)
	default:
		panic("mode unavailable. (debug, release, test)")
	}
}

Example 3

We write the above function in one more way. We use a switch case on a string value in this example, so the mode name equals the value. If it is DevMode, then gin.SetMode(gin.DebugMode).If it is ProdMode, then gin.SetMode(gin.ReleaseMode). If it is TestMode, then gin.SetMode(gin.TestMode). If none of the above three, the mode is unknown, so the function gives panic("mode unknown: " + value).

func SetMode(value string) {
	switch value {
	case DevMode:
		gin.SetMode(gin.DebugMode)
	case ProdMode:
		gin.SetMode(gin.ReleaseMode)
	case TestMode:
		gin.SetMode(gin.TestMode)
	default:
		panic("mode unknown: " + value)
	}
	modeName = value
}

Different modes in Gin

There are three different modes that we can set using the gin SetMode function. Those modes are Release Mode, Debug Mode, and Test Mode. We will read about each one of them.

  • Release Mode: In the normal production environment, we require the release mode. So, we use it for setting up the mode to the production mode.
     
  • Debug Mode: In the debug mode, Gin prints some extra debug information. So, while development, we use "debug" mode as the default mode.
     
  • Test Mode: We use the test mode in Gin's unit tests to toggle the debug mode (and the additional logging) on and off. In other words, we use it to control the log print.

Frequently Asked Questions

What is Go?

Go is a trendy procedural programming language developed in Google by Robert Griesemer, Rob Pike, and Ken Thompson in 2007.

What is Gin?

Gin is an HTTP web framework written in the Go programming language and which is high-performance in nature.

What is Gin SetMode?

SetMode is a function we use to set the gin mode according to the input string. This input string is taken while writing the SetMode function. 

How to set gin mode to release mode?

To set the gin mode to the release mode, we use the string “gin.ReleaseMode” in the SetMode function, like this, gin.SetMode(gin.ReleaseMode).

How to set gin mode to debug mode?

To set the gin mode to the debug mode, we use the string “gin.DebugMode” in the SetMode function, like this, gin.SetMode(gin.DebugMode).

Conclusion

In this article, you all learned about the Gin SetMode. We started with knowing what Go is, then we learned about Gin. After this, we learned what Gin SetMode is. Then, we saw some examples and the different modes available through the Gin SetMode. Check out our articles if you think this blog has helped you enhance your knowledge and want to learn more. Visit our website to read more such blogs. 

  1. Introduction to Go 
  2. Functions, Packages, Recursion & Closure  
  3. Go HTTP Server  
  4. Go Interface Type
  5. Go | Full Library  
     

For placement preparations, you must look at the problemsinterview experiences, and interview bundles. Enroll in our courses and refer to the mock tests and problems available; look at the Problem Sheets interview experiences, and interview bundle for placement preparations. You can also book an interview session with us.  

Consider our paid courses, however, to give your career a competitive advantage!

Happy Coding!

Live masterclass