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:
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.
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:
- Package-level variables are initialized.
- init() functions are executed.
- main() function is executed.
Multiple init() Functions
A package can contain multiple init() functions.
Example:
func init() {
fmt.Println("First init")
}
func init() {
fmt.Println("Second init")
}
Output
First init
Second init
Differences Between main() and init()
| Feature | main() | init() |
|---|---|---|
| Purpose | Entry point of the program | Package initialization |
| Package | Must be inside main package | Can be inside any package |
| Parameters | Not allowed | Not allowed |
| Return values | Not allowed | Not allowed |
| Explicitly callable | No | No |
| Number allowed | Only one per executable program | Multiple allowed |
| Execution time | Executes after initialization | Executes before main() |