On Sat, Oct 23, 2021 at 9:05 AM Michael Ellis <michael.f.el...@gmail.com> wrote:
>
> > The rules for when an initialization loop occurs are part of the language 
> > spec: https://golang.org/ref/spec#Package_initialization.
>
> Thanks for the link.  It helps with questions I've had recently about package 
> initialization.  Can you confirm that the statement
>
> "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"
>
> means that if
>  - package x imports y and z (in that order), and
>  - package y imports a
>  - package a imports b
>  - package z imports b
>
> then b will be initialized during the initialization of y ( and not 
> initialized again during the import of z)?

There is no guarantee of order of initialization of packages that have
no import relationship themselves.  The order could be as you describe
(b a y z x).  Or, the program could initialize z before y, as in (b z
a y x).  Other orderings are possible as well.  That said, it is
correct that each package will be initialized only once, as the spec
describes.

Ian

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAOyqgcX0Q6ziSk-D9pr63QvoZ%3DG6Vx-vV3NhqrdL9V9m5UuXUA%40mail.gmail.com.

Reply via email to