People search for golang const map, golang constant map, go const map, golang map const, and const map golang because other languages allow frozen map literals. In Go, a go constant map declaration is a compile-time error: const only applies to values the language spec treats as constants (booleans, numbers, strings, runes, and certain combinations). A golang global map as a var initialized once is the usual substitute, together with init, sync.Once, or small getter functions when you want constant map golang style stability from the caller’s point of view. The same rules explain why a golang constant slice cannot be a const literal either.
Tested with Go 1.24 on Linux.
Why there is no golang const map
Maps (and slices, structs containing them, and most user-defined types) are not constant types in Go. You cannot write:
const m = map[string]int{"a": 1} // compile errorThe compiler rejects that for the same reason it rejects const s = []int{1}: the value is not a constant expression. Maps are mutable reference values; making them const would require different semantics and has been discussed for years (for example in issue #22876) without changing the core const rules in Go 1.x.
If you need a mental model: const is for immutable compile-time scalars; a golang map constant in the sense of “never changes” is expressed with var, init, and encapsulation, not the const keyword.
golang global map: package-level var and init
The closest built-in pattern to a “fixed lookup table” is a package-level variable initialized with a composite literal, or assigned in init() when the table depends on other setup.
Run the snippet: the first line prints 42; the second prints true 42 (the comma ok form).
package main
import "fmt"
var codeToAnswer = map[string]int{
"life": 42,
}
func main() {
fmt.Println(codeToAnswer["life"])
v, ok := codeToAnswer["life"]
fmt.Println(ok, v)
}That map is still mutable inside the same package (codeToAnswer["x"] = 0 is legal). If you want go constant map behavior from other packages, keep the map unexported (codeToAnswer spelled with a lowercase name in real code) and expose only read functions (see below). For more on maps in general, see maps in Go and arrays and structs.
Read-only from outside the package: getters
This pattern does not make the map immutable at runtime, but it prevents other packages from mutating your table because they never receive a map value they can assign into.
package config
var labels = map[string]string{
"en": "Hello",
"es": "Hola",
}
func Label(lang string) (string, bool) {
v, ok := labels[lang]
return v, ok
}Callers use Label("en") only; they cannot assign through labels[...] from outside the config package.
Returning a map from a function
Some codebases use func Table() map[K]V that returns a new map literal on every call. That gives callers a fresh map they can change without affecting others, at the cost of allocations. A var holding one shared map avoids repeated allocation but shares the same backing map for everyone unless you copy.
golang constant slice (and const strings)
You still cannot write const s = []int{1, 2, 3}. You can keep a const string and convert when needed (the slice is created at run time, not a constant):
package main
import "fmt"
const csv = "a,b,c"
func main() {
parts := []byte(csv) // []byte built at run time from const string
fmt.Println(len(parts))
}You should see a small positive length printed (here 5 for a,b,c).
For fixed byte data at package scope, people often use var with a byte slice or populate a slice in init, same as a golang global map.
Summary
Golang const map, go constant map, and golang map const as real const declarations are not valid Go. Use a golang global map with var (and init or sync.Once when initialization is non-trivial), or functions that return maps or values, and use unexported maps with getters when you want other packages to treat the table as read-only. Golang constant slice literals are likewise not const; use var, init, or values derived from const strings or numbers. Clearer pages can help clicks when ranking and snippets already expose you to searchers, but traffic is not guaranteed to jump from edits alone.

