The init() Function Execution Order Gotcha

Enforce init() execution order by calling setup functions sequentially from a single init() function.

The init() function executes in the order it appears in the source file, but across multiple files in a package, the order is undefined and depends on the file name sorting. To enforce a specific execution sequence, use a single init() function that calls other initialization functions in the desired order.

// file_a.go
func init() {
    initStep1()
    initStep2()
}

func initStep1() { /* ... */ }
func initStep2() { /* ... */ }

This approach guarantees that initStep1 runs before initStep2, regardless of how the Go compiler orders the files.