main and init function in Golang

Last Updated : 26 Jun, 2026

The main() and init() functions are special functions in Go that control how a program starts and initializes. The init() function is used for package initialization, while the main() function serves as the entry point of an executable program.

main() function

The main() function is the entry point of an executable Go program. It is automatically executed when the program starts and is defined inside the main package.

  • Must be declared inside the main package.
  • Does not accept any parameters.
  • Does not return any value.
  • Is automatically executed by Go.
  • There can be only one main() function in an executable program.

Every executable Go program must contain exactly one main() function.

Example:

Go
package main
import (
	"fmt"
	"sort"
	"strings"
	"time"
)
func main() {
	s := []int{345, 78, 123, 10, 76, 2, 567, 5}
	sort.Ints(s)
	fmt.Println("Sorted slice:", s)
	fmt.Println("Index value:", strings.Index("GeeksforGeeks", "ks"))
	fmt.Println("Unix Time:", time.Now().Unix())
}

Output
Sorted slice: [2 5 10 76 78 123 345 567]
Index value: 3
Unix Time: 1782073293

Explanation:

  • sort.Ints() sorts the slice in ascending order.
  • strings.Index() returns the index of "ks" in "GeeksforGeeks".
  • time.Now().Unix() returns the current Unix timestamp.
  • All operations are executed inside main().

Syntax

func main() {
// code
}

init() Function

The init() function is executed automatically when a package is initialized, before the main() function runs.

It is used for tasks such as:

  • Initializing global variables
  • Loading configuration values
  • Setting up database connections
  • Registering dependencies

Example: The following code demonstrates multiple init() functions.

Go
package main
import "fmt"
func init() {
	fmt.Println("Welcome to init() function")
}
func init() {
	fmt.Println("Hello! init() function")
}
func main() {
	fmt.Println("Welcome to main() function")
}

Output
Welcome to init() function
Hello! init() function
Welcome to main() function

Explanation:

  • Two init() functions are declared.
  • Both init() functions execute automatically before main().
  • The functions execute in the order they are declared within the file.
  • After all init() functions finish, main() executes.

Syntax

func init() {
// initialization code
}

Execution Order

Go executes package initialization in the following order:

  1. Package-level variables are initialized.
  2. init() functions are executed.
  3. main() function is executed.

Multiple init() Functions

A package can contain multiple init() functions.

Example:

Go
func init() {
    fmt.Println("First init")
}
func init() {
    fmt.Println("Second init")
}

Output

First init
Second init

Differences Between main() and init()

Featuremain()init()
PurposeEntry point of the programPackage initialization
PackageMust be inside main packageCan be inside any package
ParametersNot allowedNot allowed
Return valuesNot allowedNot allowed
Explicitly callableNoNo
Number allowedOnly one per executable programMultiple allowed
Execution timeExecutes after initializationExecutes before main()
Comment
Article Tags:

Explore