Why variable y not working at package level
But z working at function level
package main
var y [2000000000 + 1]byte
func main() {
var z [2000000000 + 1]byte
_ = z
}
Why variable y not working at package level
But z working at function level
package main
var y [2000000000 + 1]byte
func main() {
var z [2000000000 + 1]byte
_ = z
}
hello and welcome to the community.
Can you exaplin better what do you mean with “y not working at package level”? and “z working at function level?”
This variation of @10IN_ALL’s code runs on my MacBook Air with no errors:
package main
var y [2000000000]byte
func main() {
var z [2000000000 + 1]byte
_ = z
}
However, this results in an error:
package main
var y [2000000000 + 1]byte
func main() {
var z [2000000000]byte
_ = z
}
Here’s the error:
./all.go:2:9: main.y: symbol too large (2000000001 bytes > 2000000000 bytes)
It appears that within the main
function, it is OK to create the variable z
with
size 2000000000 + 1
. However, outside the function, attempting to create y
, instead, with that same size causes the error.
I am just beginning to learn Go, and also am puzzled about this.
It looks like it’s a linker limitation. This discussion seems relevant.
https://groups.google.com/g/golang-nuts/c/mw5f2kyEjpA
The check appears here https://github.com/golang/go/blob/3fb1d95149fa280343581a48547c3c3f70dac5fb/src/cmd/internal/obj/objfile.go#L394
if s.Size > cutoff {
w.ctxt.Diag("%s: symbol too large (%d bytes > %d bytes)", s.Name, s.Size, cutoff)
}
The constant cutoff
is defined at line 307.
const cutoff = int64(2e9) // 2 GB (or so; looks better in errors than 2^31)
2e9 is 2,000,000,000