On Wed, Nov 14, 2018 at 4:43 PM, Tom Payne <twpa...@gmail.com> wrote: > > I couldn't find the answer to this from Googling. > > According to the documentation that I've found: > - For any given package, its imports' init() functions are run before the > package's init() function. > - Within a package, init() functions are more-or-less run in lexical > filename order. > > My question: > Can init() functions in different packages ever run concurrently, e.g. in > separate go routines, or could they be in the future? > > This seems to be within the spec, and could lead to faster startup on > multicore machines, for example when packages A and B independently depend > on C: both A and B's init() functions could run in separate goroutines after > C's init() function, without violating the above. However, this could cause > problems if (for example) A and B's init() functions update a map in C > without synchronization.
See https://golang.org/ref/spec#Package_initialization : "Package initialization—variable initialization and the invocation of init functions—happens in a single goroutine, sequentially, one package at a time." 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. For more options, visit https://groups.google.com/d/optout.