Golang Tutorial

Fundamentals

Control Statements

Functions & Methods

Structure

Arrays & Slices

String

Pointers

Interfaces

Concurrency

main and init function in Golang

In Go, both the main() and init() functions have special significance, serving unique roles in the program lifecycle.

1. main() function:

The main() function serves as the entry point to a Go program. When you run a Go executable, this is the function that gets executed first.

  • Every Go program must have one and only one main() function.
  • It must reside in the main package.
  • It doesn't take any arguments and doesn't return any values.

Example:

package main

import "fmt"

func main() {
    fmt.Println("Hello, Golang!")
}

2. init() function:

The init() function is used to perform initialization tasks. It's optional but can be very useful.

  • If present, it runs before the main() function.
  • A package can have multiple init() functions (either in a single file or distributed across multiple files), and they are called in the order they are presented.
  • Like main(), the init() function doesn't take any arguments and doesn't return any values.

Example:

package main

import "fmt"

func init() {
    fmt.Println("This will get printed first!")
}

func main() {
    fmt.Println("This will get printed second!")
}

Using both main() and init():

If you combine the usage of both main() and init(), the init() function(s) will always run before the main() function. This is true even if the init() function is located in a file after the main() function (when considering alphabetical order).

Example:

Consider two files in the main package:

a.go:

package main

import "fmt"

func init() {
    fmt.Println("Init from a.go")
}

func main() {
    fmt.Println("Main function")
}

b.go:

package main

import "fmt"

func init() {
    fmt.Println("Init from b.go")
}

If you run the program, the output will be:

Init from a.go
Init from b.go
Main function

This behavior can be utilized to perform specific initializations in packages before any other code runs, such as setting up default values, initializing data structures, or setting up connections.

Key Takeaways:

  • The main() function is the program's entry point, and every executable Go program must have it.
  • The init() function is optional and serves to handle initialization tasks. If it exists, it runs before main().
  • A package can have multiple init() functions, and they will run in the order they appear.

By understanding and utilizing both main() and init(), you can effectively control the initialization and execution order of your Go programs.

  1. Golang main function explanation:

    • Description: The main function in Golang is the entry point for the execution of a program. It is the first function that gets executed when a Golang program is run.

    • Code:

      package main
      
      import "fmt"
      
      // main function as the entry point
      func main() {
          fmt.Println("Hello, Golang!")
      }
      
  2. Golang init function usage:

    • Description: The init function in Golang is a special function that is called automatically before the main function. It is used for package-level initialization.

    • Code:

      package main
      
      import "fmt"
      
      // init function for package-level initialization
      func init() {
          fmt.Println("Initializing the package")
      }
      
      // main function as the entry point
      func main() {
          fmt.Println("Hello, Golang!")
      }