I can't find any information on this that's explicit.
Let's say you have a package called "apiWrapper" and in it you have a global struct called "Config" that gets set by "init()". "Config" has a string field called "ApiKey" and is initially set to "none".
Now in a separate package, let's call it package "Foo", you have file A.go and file B.go each include package "apiWrapper". In A.go, it sets "apiWrapper.Config.ApiKey = "xyz"" and in B.go, it reads "apiWrapper.Config.ApiKey" for whatever reason. If A.go is executed first, would B.go read it as "none" or "xyz"?
And also, how about instead of being files, A and B are subpackages to Foo, would B still read it as "none" or "xyz"?
I guess the real question at hand is, per executable, when you include a package in multiple locations, is it one "entity" (for lack of a better term) that everywhere references or does each location get its own "copy" of that package?
评论:
cs-guy:
klaaax:From the language spec:
If a package has imports, the imported packages are initialized before initializing the package itself. If multiple packages import a package, the imported package will be initialized only once.
mwholt:init is executed only once. Try to avoid globals as much as possible anyway, I never found a good case for init. Maybe someone can come up with one.
petejodo:I use init to initialize an unexported
map[string]func()
where the (relatively simple) functions in the map call functions that end up calling other functions in the map. Doing this outside of init creates an initialization loop error.
init()
is also nice for using the flag package outside the flow of yourmain()
so you can keep it clean.
Azzk1kr:yeah you're right, it was just an easy way to express my question which revolved more around how packages worked. I somehow never came across the section of the language spec that cs-guy posted above so my mental model of packages has been shaky at best
DavidDavidsonsGhost:This stacksignal package was one interesting thing which makes use of init(). Just import the package, and you'll get a signal handler on SIGUSR1.
Its nice and useful to use init to add modules or plugins to other module must by importing the plugin in your main. If you look at the image decoders in the standard library you will see an example of this.
