Skip to content

doc/spec: Package initialization order: initialized order d b c a, real result is d c b a #31724

Closed
@terryding77

Description

@terryding77

the initialization order is <code>d</code>, <code>b</code>, <code>c</code>, <code>a</code>.

the code as spec#Package_initializationthe

package main

import "fmt"

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

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

func main() {
    fmt.Println("a", a)
    fmt.Println("b", b)
    fmt.Println("c", c)
    fmt.Println("d", d)
}

got

result:
a 9
b 5
c 4
d 5

but with the order in spec d-> b -> c -> a, I think the expect result shoud be

a 9
b 4
c 5
d 5

if I change a = c + b to a = b + c the order is same as spec init order d b c a
so is there something wrong?

my go version is go version go1.12.4 linux/amd64

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions