Constant Arrays in Go: How to Declare Them Without Losing Your Mind

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: The Eternal Stack Overflow Question

\n\n

If you've ever Googled "Declare a constant array in Go", you've probably seen that very Stack Overflow thread with thousands of views. And yes, you are not alone in your confusion. In Go, you really cannot simply write const arr = [...]int{1, 2, 3} — the compiler will politely tell you "no".

\n\n

Why is that? And what should you do if you need an immutable list of data? In this article, we'll explore the nature of constants in Go, understand why arrays cannot be constants, and find the best workarounds. Spoiler: there is a solution, and it's even more elegant than you think.

\n\n

Why Doesn't Go Allow Declaring a Constant Array?

\n\n

To understand this, we need to look under the hood of the language. In Go, constants are immutable values computed at compile time. They must be strictly typed and known before the program runs. Arrays, on the other hand, are composite data types that occupy memory and can be modified.

\n\n

Here's a simple example that will NOT compile:

\n\n
package main\n\nconst arr = [3]int{1, 2, 3} // Error: const initializer [3]int{1, 2, 3} is not a constant\n\nfunc main() {\n    fmt.Println(arr)\n}
\n\n

The compiler says: "Hey, that's not a constant!" And it's right. Even if the array is declared as const, Go does not guarantee that its contents won't be changed via pointers or slices. Therefore, the language designers decided: no constant arrays.

\n\n

But don't be disappointed just yet. There are at least three ways to work around this limitation, and each has its advantages.

\n\n

Method 1: Using a Variable with a "Don't Touch" Comment

\n\n

The simplest and most naive way is to just declare a variable and hope no one changes it. But we're serious developers, right?

\n\n
package main\n\nimport "fmt"\n\n// arr is a constant array. Please don't change it.\nvar arr = [3]int{1, 2, 3}\n\nfunc main() {\n    fmt.Println(arr)\n    // arr[0] = 100 // Uncomment this — and everything breaks\n}
\n\n

The downsides are obvious: this is not a real constant. Anyone can change the array via assignment. But for quick prototypes or small projects, it'll do.

\n\n

Method 2: Getter Function (The Right Way)

\n\n

This approach is used in production code. We hide the array inside a function and return a copy of it. This way, no one can modify the original.

\n\n
package main\n\nimport "fmt"\n\n// getColors returns a copy of the colors array.\n// The array is protected from external modification.\nfunc getColors() [3]string {\n    return [3]string{"Red", "Green", "Blue"}\n}\n\nfunc main() {\n    colors := getColors()\n    fmt.Println(colors)\n    // colors[0] = "Yellow" // This changes the copy, not the original\n}
\n\n

Why is this good? Because:

\n
    \n
  • The array cannot be modified from outside — the function returns a copy.
  • \n
  • The code is readable and understandable.
  • \n
  • Easy to test and maintain.
  • \n
\n\n

But there's a nuance: each function call creates a new copy of the array. If the array is large (e.g., 1000 elements), this can be costly. In that case, use the following method.

\n\n

Method 3: Package-Level Variable with Private Access

\n\n

If you are writing a package and want to protect the array from modification, make it unexported (lowercase) and provide access through a function.

\n\n
package constants\n\n// The array is only accessible within the package\nvar defaultPorts = [5]int{80, 443, 8080, 3000, 5000}\n\n// GetDefaultPorts returns a copy of the ports array.\nfunc GetDefaultPorts() [5]int {\n    return defaultPorts\n}
\n\n

Now users of your package can only read the data, but not modify it. Perfect for libraries.

\n\n

Method 4: Using iota for Constant Sets

\n\n

What if you don't need the values themselves, but rather their indices? For ex

Blogs

Book Recommendations