I recently found the code below:
var noEscape = [256]bool{
'A': true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
'a': true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
'0': true, true, true, true, true, true, true, true, true, true,
'-': true,
'.': true,
'_': true,
'~': true,
}
I understand that this [N]bool
is initialized with N false
(zeros) by default. And if the index followed by a colon is specified values can are defined form the index (inclusive). Where is it described? What types can be used as an index value (there is a char
in the example)?
For an array or slice literal, the index values must be constants. From the docs on "composite literals"
For array and slice literals the following rules apply:
- Each element has an associated integer index marking its position in the array.
- An element with a key uses the key as its index; the key must be a constant integer expression.
- An element without a key uses the previous element's index plus one. If the first element has no key, its index is zero.
The literal characters in the example are untyped constants, which just happen to be written as a single rune literal. If you were to assign any of those values to a variable to use as the index, the code would not compile.