Skip to content

spec: incorrect initialization order  #12369

@bigwhite

Description

@bigwhite

version: go version go1.5 darwin/amd64

There is an example about variable initialization order in the go specs:

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

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

the spec gives the initialization order is d, b, c, a. but the result of above example is not the same as the order spec gives.

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, b, c, d)
}

$go run example.go
9 5 4 5

according to the result, the actual init order should be d ,c ,b, a, not the same as the order in spec.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions