Re: [go-nuts] Unexpected order of global variable declaration during package initialization

2022-03-24 Thread Brian Candler
On Thursday, 24 March 2022 at 12:35:38 UTC tapi...@gmail.com wrote: > Aha, sorry, the file order really matters here. > But for this specified case, it should not. > > That's not right. I remembered in an issue thread, it is mentioned that > the order should be > > 1. D is not ready (because it

Re: [go-nuts] Unexpected order of global variable declaration during package initialization

2022-03-24 Thread tapi...@gmail.com
Aha, sorry, the file order really matters here. But for this specified case, it should not. That's not right. I remembered in an issue thread, it is mentioned that the order should be 1. D is not ready (because it depends on A) 2. A is ready, so it is initialized: it gets value 3. 4. B is

Re: [go-nuts] Unexpected order of global variable declaration during package initialization

2022-03-24 Thread Joao Carlos
Thank you all for the quick replies and helpful comments. I opened an issue in the go's issue tracker (https://github.com/golang/go/issues/51913). On Thursday, 24 March 2022 at 13:23:14 UTC+1 Brian Candler wrote: > Ugh, quoting got broken there. > > $ go run rewritten_f1.go f2.go > Init A >

Re: [go-nuts] Unexpected order of global variable declaration during package initialization

2022-03-24 Thread Brian Candler
Ugh, quoting got broken there. $ go run rewritten_f1.go f2.go Init A Init B Init C 1 4 3 $ go run f2.go rewritten_f1.go Init A Init B Init C 1 2 1 Hopefully that will show properly. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To

Re: [go-nuts] Unexpected order of global variable declaration during package initialization

2022-03-24 Thread Brian Candler
On Thursday, 24 March 2022 at 12:15:16 UTC tapi...@gmail.com wrote: > BTW, the rewritten version outputs > > Init A > Init B > Init C > 1 4 3 > > On my machine (go1.18 linux/amd64). > It depends on the order, and the OP was positing what happens when f2.go is presented first to the

Re: [go-nuts] Unexpected order of global variable declaration during package initialization

2022-03-24 Thread Brian Candler
On Thursday, 24 March 2022 at 11:30:37 UTC tapi...@gmail.com wrote: > > 2. A < D < B < C - happens when f2.go is passed first to the compiler. > In this case, the *expected output *would be "1 2 1". However, the *actual > output* is "1 2 3". > > This is not true by my understanding of the spec.

Re: [go-nuts] Unexpected order of global variable declaration during package initialization

2022-03-24 Thread tapi...@gmail.com
BTW, the rewritten version outputs Init A Init B Init C 1 4 3 On my machine (go1.18 linux/amd64). On Thursday, March 24, 2022 at 7:30:37 PM UTC+8 tapi...@gmail.com wrote: > > 2. A < D < B < C - happens when f2.go is passed first to the compiler. > In this case, the *expected output *would

Re: [go-nuts] Unexpected order of global variable declaration during package initialization

2022-03-24 Thread tapi...@gmail.com
> 2. A < D < B < C - happens when f2.go is passed first to the compiler. In this case, the *expected output *would be "1 2 1". However, the *actual output* is "1 2 3". This is not true by my understanding of the spec. https://go.dev/ref/spec#Package_initialization If f2.go is passed first,

Re: [go-nuts] Unexpected order of global variable declaration during package initialization

2022-03-23 Thread Ian Lance Taylor
On Wed, Mar 23, 2022 at 2:01 PM Joao Carlos wrote: > > I'm currently observing a behavior in the package initialization that looks > incompatible with the Go language specification. > Let's consider the following two .go files which are in the same package > > f1.go > package main > > var A int

[go-nuts] Unexpected order of global variable declaration during package initialization

2022-03-23 Thread Joao Carlos
Hi all, I'm currently observing a behavior in the package initialization that looks incompatible with the Go language specification. Let's consider the following two .go files which are in the same package *f1.go* package main var A int = 3 var B int = A + 1 var C int = A