Package initialization
suggest changePackage can have one or more init
methods.
Those methods are run only once before main
function is executed.
package foo
func init() {
// init code
}
If you just want to run the package initialization without referencing anything from it use the following import expression.
import _ "foo"
Package initialization function can be used to create initial state needed by code in the package.
Avoid temptation of doing too much in init
function. Such implicit logic negatively impacts other people’s ability to understand the code.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents