Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spec: package initialization issue #25474

Closed
Happyholic1203 opened this Issue May 21, 2018 · 5 comments

Comments

Projects
None yet
6 participants
@Happyholic1203
Copy link

Happyholic1203 commented May 21, 2018

The following piece of code produces different result from SPEC

$ cat main.go
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 main.go
9 5 4 5

Following the SPEC, b should be initialized before c, so instead of 9 5 4 5, 9 4 5 5 is expected, according to the SPEC:

... package-level variables are initialized in declaration order but after any of the variables they depend on.

However, if we change a = c + b to a = b + c, then b is initialized before c.

So it seems like the order of presence precedes the order of declaration, deviating the SPEC.

go version: 1.10.2 darwin/amd64

go env:

GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/user/Library/Caches/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/user/go"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.10.2/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.10.2/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/d3/8sk_rdjx1s703xh89gb4x38h0000gn/T/go-build710804438=/tmp/go-build -gno-record-gcc-switches -fno-common"
@meirf

This comment has been minimized.

Copy link
Contributor

meirf commented May 21, 2018

I'm able to reproduce this going as far back as go1.7 on darwin/amd64 mac os 10.13.3 (Might also reproduce on earlier go versions but haven't tried older versions yet.)

2 points of clarification on the original post:

@Happyholic1203

This comment has been minimized.

Copy link
Author

Happyholic1203 commented May 21, 2018

The above 2 points of clarification are valid, thanks.
Sorry for the wrong URL.

@mvdan

This comment has been minimized.

Copy link
Member

mvdan commented May 21, 2018

@agnivade agnivade changed the title Package initialization not following SPEC spec: package initialization issue May 21, 2018

@agnivade agnivade added this to the Go1.11 milestone May 21, 2018

@bcmills

This comment has been minimized.

Copy link
Member

bcmills commented May 21, 2018

Please use playground links when posting runnable code: https://play.golang.org/p/PzHf02Fah3q

@griesemer

This comment has been minimized.

Copy link
Contributor

griesemer commented May 21, 2018

Duplicate of #22326.

@griesemer griesemer closed this May 21, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.