Skip to content

cmd/compile: incorrect initialization order #9094

@paranoiacblack

Description

@paranoiacblack
$ cat incorrect_init.go
package main

var (
    a = p("a") + c + b
    b = p("b") + f()
    c = p("c") + f()
    d = p("d") + 3
)

func f() int {
     d++
     return d
}

func p(s string) int {
     println(s)
     return 0
}

func main() {
    println(a, b, c, d)
}

compiling and executing produces:

$ go run incorrect_init.go
d
c
b
a
9 5 4 5

But the correct order per the spec should be: d, b, c, a, with values 9, 4, 5, 5.

See: http://tip.golang.org/ref/spec#Package_initialization

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions