Download our all in one automation tool for various social media websites.
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")
}
Above program produces following output:
init is called
main is called
As demonstrated in above program, main is called before init is called.
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.