Go "non-constant array bound" - Fix Array Sizes in CI
Go array lengths must be compile-time constants. Sizing an array with a variable (or any non-constant expression) is rejected - a slice is the right tool for a runtime-sized sequence.
What this error means
The build fails with non-constant array bound n (or array bound must be constant), pointing at the array declaration. It is deterministic and appears when an array size comes from a variable.
./buffer.go:7:13: non-constant array bound n
# from: var buf [n]byte // n is a variable, not a constantCommon causes
Array length set from a variable
A declaration like [n]byte where n is a variable cannot be sized at compile time, so Go rejects it.
A length from a non-constant expression
Even an expression that looks fixed (a function call, a non-const value) is not a constant, so it cannot be an array bound.
How to fix it
Use a slice for a runtime-sized sequence
Slices are sized at runtime - make([]T, n) is the idiomatic replacement for a variable-length array.
buf := make([]byte, n) // instead of: var buf [n]byteUse a const if the size is truly fixed
When the length is a compile-time value, declare it as a constant so it is a valid array bound.
const size = 256
var buf [size]byteHow to prevent it
- Use slices (
make([]T, n)) for any length known only at runtime. - Reserve arrays for fixed, compile-time-constant sizes.
- Declare fixed sizes as
constso they can bound arrays.