init() function

Let's learn about Go's init() function

init() function

  • init() function looks similar to main function, but doesn't take arguments or return values

  • init() function is the first function called when a package is loaded

    • Use it when you need package initialization logic!

When is the init() function run?

It executes in the order: import --> const --> var --> init()

Last updated