4 ms·
It's slightly more complicated than that: In addition to the init() function, variables on the package level can be initialized during initialization too. For
by Laremere 12y ago
It's slightly more complicated than that: In addition to the init() function, variables on the package level can be initialized during initialization too.
For example: http://play.golang.org/p/tvlX7cqaaB http://play.golang.org/p/tvlX7cqaaB
This is important for package level variables such as arrays. Since there are only a very limited number of types which can be constants in Go, everything else which you'd use like a constant needs to be initialized.
- leaveyou 12y agoIf we go in details, we can also add that goroutines created in init() or at init time are effectively started only after all init() functions finish.