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?
1 answers
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.
Hot tools Tags
Hot Questions
Popular tool
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 phpstudy integrated installation environment runtime library
PHP programmer toolbox full version
Programmer Toolbox v1.0 PHP Integrated Environment
VC11 32-bit
VC11 32-bit phpstudy integrated installation environment runtime library
SublimeText3 Chinese version
Chinese version, very easy to use
Hot Topics
20532
7
13639
4






