A new product!

Social Media Toolkit

Download our all in one automation tool for various social media websites.

Gopher

Main package and main function in Golang

Published on 10 May 2020
Last Updated on 10 May 2020

In Golang main package is a special package. A Go program starts executing at the main package.

The main package also contains the main function. main function is an entry point to a Go program.

Inside main package, init function is called before the main function is called, this behaviour is demonstrated in the example given below:

// program to demonstrate that init is called before main
package main

import "fmt"

func init()  {
	fmt.Println("init is called")
}

func main(){
	fmt.Println("main is called")
}

Program output

Above program produces following output:

init is called
main is called

Program Description

As demonstrated in above program, main is called before init is called.

Main function

main package contains a special function called the main function. main function is the entry point to executable programs. main function does not accept any arguments nor does it return any value. Every executable program must contain a single main package and main function.

Please refer to other articles to understand how init function works.