search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

1 answers
Q&A Why doesn't Golang allow const maps?
Why doesn't Golang allow const maps?
P粉994092873 2025-11-28 16:22:31 Views 113

0

I would like to create a constant map like the following:

const (
    running = map[string]string{
        "one": "ONE",
        "two": "TWO",
    }
)

however whenever I do I get the following error:

const initializer map[string]string literal is not a constant

Why is this the case, why does Golang not treat them like any other variable?

Your Answer
submit

1 answers
0

From https://golang.org/ref/spec#Constants:

A constant value is represented by a rune, integer, floating-point, imaginary, or string literal, an identifier denoting a constant, a constant expression, a conversion with a result that is a constant, or the result value of some built-in functions such as unsafe.Sizeof applied to any value, cap or len applied to some expressions, real and imag applied to a complex constant and complex applied to numeric constants.

tl;dr only numeric types, strings and bools can be constants, arrays, slices and maps aren't a numeric type.

2025-12-02 22:00:57

submit

Popular tool

vc9-vc14 (32+64 bit) runtime library collection (link below)

vc9-vc14 (32+64 bit) runtime library collection (link below)

Download the collection of runtime libraries required for phpStudy installation

VC9 32-bit

VC9 32-bit

VC9 32-bit phpstudy integrated installation environment runtime library

PHP programmer toolbox full version

PHP programmer toolbox full version

Programmer Toolbox v1.0 PHP Integrated Environment

VC11 32-bit

VC11 32-bit

VC11 32-bit phpstudy integrated installation environment runtime library

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use