Go init() Functions Explained

2025-01-153 minTech Related
#golang

Automatic execution of init() before main().

Go automatically runs init() before main().

Example:

func init() {
    fmt.Println("init runs first")
}

func main() {
    fmt.Println("main runs second")
}

Output: init runs first
main runs second