Skip to content
Latchkey

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.

go build output
./buffer.go:7:13: non-constant array bound n
# from: var buf [n]byte   // n is a variable, not a constant

Common 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.

Go
buf := make([]byte, n)   // instead of: var buf [n]byte

Use 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.

Go
const size = 256
var buf [size]byte

How 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 const so they can bound arrays.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →