Understanding the init Function in Go: Purpose, Execution, and Best Practices

Go init Function

In Go, the init function is a special function that is automatically executed before the main function when a package is initialized. It is primarily used for setup tasks, such as initializing global variables, opening datab…


This content originally appeared on DEV Community and was authored by Md Abu Musa

Go init Function

In Go, the init function is a special function that is automatically executed before the main function when a package is initialized. It is primarily used for setup tasks, such as initializing global variables, opening database connections, or registering dependencies.

Key Characteristics of init Function:

  1. No Arguments & No Return Value – The init function does not take parameters or return values.
  2. Executed Automatically – It runs before main() and does not require explicit invocation.
  3. Can Have Multiple init Functions – A package can have multiple init functions, even across different files.
  4. Executed in Declaration Order – If multiple init functions exist in a package, they are executed in the order in which they appear.

Example 1: Basic init Usage

package main

import "fmt"

func init() {
    fmt.Println("Initializing...")
}

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

Output:

Initializing...
Main function running...

✅ The init function runs before main().

Example 2: Using init for Global Variable Initialization

package main

import "fmt"

var config string

func init() {
    config = "Application Configured"
    fmt.Println("Configuring application...")
}

func main() {
    fmt.Println(config) // Output: Application Configured
}

Example 3: init in Multiple Files

📌 If a package has multiple files, all init functions run before main(), in the order they appear.

File 1 (a.go)

package main

import "fmt"

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

File 2 (b.go)

package main

import "fmt"

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

Output (execution order is preserved):

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

Example 4: init in a Different Package

package utils

import "fmt"

func init() {
    fmt.Println("Initializing utils package...")
}

func SayHello() {
    fmt.Println("Hello from utils!")
}

Main File (main.go)

package main

import (
    "fmt"
    "your_project/utils"
)

func init() {
    fmt.Println("Initializing main package...")
}

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

Output (Package-Level Execution Order)

Initializing utils package...
Initializing main package...
Main function running...
Hello from utils!

✅ The init function in imported packages runs before the init function in main.

When to Use init?

Good Use Cases:

  • Initializing global variables.
  • Setting up logging configurations.
  • Registering dependencies (e.g., database connections).
  • Ensuring required setup before main() runs.

Avoid Using init for:

  • Complex logic (prefer explicit initialization in main).
  • Business logic (should be in main or other functions).

Summary

  • init() runs automatically before main().
  • It has no parameters and no return values.
  • Each package can have multiple init functions.
  • init in imported packages runs before init in main.


This content originally appeared on DEV Community and was authored by Md Abu Musa


Print Share Comment Cite Upload Translate Updates
APA

Md Abu Musa | Sciencx (2025-02-22T12:33:36+00:00) Understanding the init Function in Go: Purpose, Execution, and Best Practices. Retrieved from https://www.scien.cx/2025/02/22/understanding-the-init-function-in-go-purpose-execution-and-best-practices/

MLA
" » Understanding the init Function in Go: Purpose, Execution, and Best Practices." Md Abu Musa | Sciencx - Saturday February 22, 2025, https://www.scien.cx/2025/02/22/understanding-the-init-function-in-go-purpose-execution-and-best-practices/
HARVARD
Md Abu Musa | Sciencx Saturday February 22, 2025 » Understanding the init Function in Go: Purpose, Execution, and Best Practices., viewed ,<https://www.scien.cx/2025/02/22/understanding-the-init-function-in-go-purpose-execution-and-best-practices/>
VANCOUVER
Md Abu Musa | Sciencx - » Understanding the init Function in Go: Purpose, Execution, and Best Practices. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/02/22/understanding-the-init-function-in-go-purpose-execution-and-best-practices/
CHICAGO
" » Understanding the init Function in Go: Purpose, Execution, and Best Practices." Md Abu Musa | Sciencx - Accessed . https://www.scien.cx/2025/02/22/understanding-the-init-function-in-go-purpose-execution-and-best-practices/
IEEE
" » Understanding the init Function in Go: Purpose, Execution, and Best Practices." Md Abu Musa | Sciencx [Online]. Available: https://www.scien.cx/2025/02/22/understanding-the-init-function-in-go-purpose-execution-and-best-practices/. [Accessed: ]
rf:citation
» Understanding the init Function in Go: Purpose, Execution, and Best Practices | Md Abu Musa | Sciencx | https://www.scien.cx/2025/02/22/understanding-the-init-function-in-go-purpose-execution-and-best-practices/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.