init
it’s a reserved special name in go, it
- can only be a func with no returns or args
- cannot be used a variable name ( same as any reserved keyword )
has to be this
func init(){
// init code
}interestingly you can define an init func multiple times even in the same file
exec order?
- same file is order of appearance
- diff files but same package is some compiler generated order not to be relied on
- across packages? a dependency chain is followed and deepest dependencies are initialised first. so if main imports a imports b then order of init is b → a → main
total order?
def init_package:
1. package level vars init
2. init funcs
1. run init_package on any imported packages
2. run main.main()