Init Function
This is another special-purpose function of the golang and is used to create such programs that maintain the stable code execution flow.
The features are:
- Firstly, the package's initialization occurs, and all the packages contain this function.
- Execution is before the main function.
- Declaration of multiple init functions in a program is allowed.
- These functions are initialized based on their lexical order.
-
The function can not be initialized explicitly, and even if used, you might get an error.
Syntax
The syntax for the init function is-
func init() {
//code to be executed
}
Example
//main package declaration
package main
//importing packages
import "fmt"
//init function
func init(){
fmt.Println("Hello!")
}
//main function
func main(){
fmt.Println("Welcome to Coding Ninjas")
}
Output:
Hello!
Welcome to Coding Ninjas
Order of Execution
The order of execution is -
- All the required packages are imported into the program.
- Further, all the imported packages are initialized.
- All the constant variables are then initialized.
- All the variables are initialized.
- Lastly, the init functions are executed.
Example
In this example, we use the main package as the golang_func. It further contains two packages, namely the first and second. These packages have their Golang files. Also, the golang_func has main.go file which imports the other packages.
Steps to create package -
- The most important requirement is to check the GOPATH in environment variables and set it to the directory which contains all files.
- A new folder needs to be created with the package's name you want to have.
- Create two folders as required in our example.
- Create your go files as required.
- After writing the file in the package, compile this code using the go compiler.
- Open the command prompt in the directory where all your packages are located and run the following command-
go install
7. Now, create your main.go file.

Fig. File Structure
Commands to create the package for this example -
- Go inside the golang_func directory and run the following command to create a go module named golang_func.
go mod init golang_func
2. A file named go.mod will be created using the above command. The content of the file is as follows-
module golang_func
go 1.18.
3. Now, write all the code according to the file structure.
Code:
main.go
package main
import _"golang_func/first"
import _"golang_func/second"
//global variable
var x = fun()
func fun() int{
println("This is fun().")
return 1
}
func init(){
println("Inside main.go init function.")
}
func main(){
println("Inside the main function.")
println("Value of x is ",x)
}
first_a.go
package first
import _"golang_func/second"
func init(){
println("Inside first_a init function.")
}
first_b.go
package first
import _"golang_func/second"
func init(){
println("Inside first_b init function.")
}
second_a.go
package second
func init(){
println("Inside second_b init function.")
}
Output:
Inside second_b init function.
Inside first_a init function.
Inside first_b init function.
This is fun().
Inside main.go init function.
Inside the main function.
Value of x is 1
Explanation:
- The first package is imported by the main.go import statement.
- The files are loaded in Golang according to their lexical order, due to which first_a.go will be loaded and then first_b.go.
- These files further import package ‘second’ and then in this second_a.go file the init function is called.
- Even if both first_a.go and first_b.go are importing packages second still init function in second_a.go is called only once. This is one of the features of the init function.
- To summarize, the order of execution of init statements is-
second_b.go -> first_a.go -> first_b.go -> main.go
- Order of execution in main.go -
loading packages -> global initialization -> init function -> main function
Frequently Asked Questions
Can the init function be initialized after the main function?
As execution is according to the lexical order, these can be initialized anywhere in the code.
How often can the program's main() function be initialized?
The main function can be initialized only once in the program.
What are the drawbacks of using the init function?
Using it a lot of times reduces the code's readability and creates problems during the testing of the code.
Conclusion
In this article, we have extensively discussed on Main and Init functions in the Golang. We start with a brief introduction, then discuss their features and examples. Also, we learned about the order of execution of these functions.
After reading about these functions, are you not feeling excited to read more articles on the topic of Golang? Don’t worry; Coding Ninjas has you covered. To learn, see Go string, Go arrays, Go select, Go data types and operators, Go variable, types, and constants, Go identifier and keywords, Go routines, and many more.
Do upvote our blogs if you find them helpful and engaging!
Happy Learning!