golang / tour

[mirror] A Tour of Go
BSD 3-Clause "New" or "Revised" License
1.53k stars 521 forks source link

tour: Struct literals example is confusing #1595

Open tdurtschi opened 4 months ago

tdurtschi commented 4 months ago

Context: https://go.dev/tour/moretypes/5

The print statement should log each Vertex in the order it was created. I found the output very confusing until I realized the structs were shuffled in the call to Println.

var (
    v1 = Vertex{1, 2}  // has type Vertex
    v2 = Vertex{X: 1}  // Y:0 is implicit
    v3 = Vertex{}      // X:0 and Y:0
    p  = &Vertex{1, 2} // has type *Vertex
)

func main() {
    fmt.Println(v1, p, v2, v3) // Should be fmt.Println(v1, v2, v3, p)
}