Package initialization
suggest changeA package can have one or more init functions.
Those functions are run only once, after the packages they import are initialized and before the 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"
An init function can create initial state needed by code in the package.
Avoid the temptation of doing too much in an 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