4 ms·
Function main() in package "main" is the entry point for a Go application but it is not the first code that gets executed when you start a Go app. Each package
by leaveyou 12y ago
Function main() in package "main" is the entry point for a Go application but it is not the first code that gets executed when you start a Go app. Each package can have an init() function which gets executed in the order of dependency (if pkg A imports pkg B then B.init() is executed before A.init()) before main.main() is executed.
- canadev 12y agoI see. Thank you.
- Laremere 12y agoIt'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.