I was interested to learn about the order in which there are several types of expressions, so I tried this code at the top level of the ad, thinking that it would fail, but found that it works:
http://play.golang.org/p/CfP3DEC5LP
var x = func() *Foo { fmt.Println(f) // prints &{foobar} return f }() var f = &Foo{"foobar"} type Foo struct { bar string }
Note:
The type Foo struct declaration is at the bottom
before type declaration there is var f declaration and &Foo{] assignment
before the var declaration, there is a function that is called immediately, which refers and returns the variable f .
While it didn't surprise me too much that I could make the value &Foo{} even if it had a place before the type Foo struct declaration, it surprised me that I could successfully reference and print the value f before its assignment.
Is this reliable and specific behavior? I could not find any reference to such an order in the specification, but perhaps I did not notice it.
source share