go - Function variable representing init -


in go, can define multiple init functions in given package, of run prior execution in unspecified order. 1 consequence of having multiples of such functions it's impossible call or identify them in normal code. example, following not compile:

func main() {     fmt.println(init) } func init() { } 

(see here go playground example) question - advantage being able have multiple init functions give, , if there weren't multiple init functions, able reference or call init functions?

advantage of being able have multiple init functions imo improves readability locality: can write initialization function next stuff being initialized , not remotely if have centralize init functions one. which, btw, in different source file.

taking function pointer of hypothetical per-package-single init function prohibited well. reason having such pointer allow, in cases, call init function "out of order", ie. before running dependencies - other init functions in other packages. break guarantees.


Comments