Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
The Main function
Init Function
Order of Execution
Frequently Asked Questions
Can the init function be initialized after the main function?
How often can the program's main() function be initialized?
What are the drawbacks of using the init function?
Last Updated: Mar 27, 2024

Main and Init Functions in Golang

Author Gurleen Kaur
0 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM


There are two functions in the golang that are reserved and used for special purposes only. These functions are namely main and init. Both are powerful in their way and used to simplify your code.

Let’s dive deeper into the functions and understand how to use them, when to use them, and why.

The Main function

This function is the most basic function of the Golang, and its primary purpose is to act as the entry point for the executable code or programs. This main function is under the main package of the Golang, and it is reserved. 

This main package can be defined as a wrapper that wraps all the executable code as one, and then this set of code can be used in any program just by importing the package.

Following are some important points regarding these functions-

  • It is a predefined function.
  • There is no need for an explicit call for this function as it is called on its own.
  • This function doesn’t return anything, nor does it take any arguments.
  • There must exist only one main function in a program with executable code.
  • The program ends if the main function returns.

In the example below, we have done the following-

  1. Declaration of the main package which binds the code.
  2. Importing the “fmt” package. This package is a built-in package in Golang and consists of all the print operations, scan operations, etc.


//main package declaration
package main
// importing package
import "fmt"
// main function
func main(){
     fmt.Println("Hello! Welcome to Coding Ninjas")


Hello! Welcome to Coding Ninjas
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job

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.


The syntax for the init function is-

func init() { 
//code to be executed


//main package declaration
package main
//importing packages
import "fmt"
//init function
func init(){
//main function
func main(){
    fmt.Println("Welcome to Coding Ninjas")


Welcome to Coding Ninjas

Order of Execution

The order of execution is -

  1. All the required packages are imported into the program.
  2. Further, all the imported packages are initialized.
  3. All the constant variables are then initialized.
  4. All the variables are initialized.
  5. Lastly, the init functions are executed.


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 -

  1. The most important requirement is to check the GOPATH in environment variables and set it to the directory which contains all files.
  2. A new folder needs to be created with the package's name you want to have.
  3. Create two folders as required in our example.
  4. Create your go files as required.
  5. After writing the file in the package, compile this code using the go compiler.
  6. 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 -

  1. 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.



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)


package first
import  _"golang_func/second"
 func init(){
   println("Inside first_a init function.")


package first
import  _"golang_func/second"
 func init(){
   println("Inside first_b init function.")


package second
func init(){
   println("Inside second_b init function.")


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


  1. The first package is imported by the main.go import statement.
  2. 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.
  3. These files further import package ‘second’ and then in this second_a.go file the init function is called.
  4. 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.
  5. To summarize, the order of execution of init statements is-

second_b.go -> first_a.go -> first_b.go -> main.go

  1. 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.


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 stringGo arraysGo selectGo data types and operatorsGo variable, types, and constantsGo identifier and keywordsGo routines, and many more.

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass