How to Use the init() Function in Go

The init function is a special Go function that runs automatically before main to initialize package state.

There is no init() function in the Go standard library; init is a special function that runs automatically before main to set up package state. Define it with no arguments and no return value in any .go file within a package to execute setup code at initialization time.

func init() {
    // Setup code runs before main()
    fmt.Println("Package initialized")
}