Description
This will probably be assigned a Go2 label, although I think that it could be done during Go 1.
Consider multiline string constants:
const multiline = `
This is a long text
that spans multiple lines
and then ends.
`
If you feed this text to a parser and it finds a problem on the first line ("This is a long text"), it will actually report that the error is on the second line because of the leading newline. One might think that this is easily solved by slicing the string:
const multiline = `
This is a long text
that spans multiple lines
and then ends.
`[1:]
But this won't compile. The Spec currently says:
(…) For untyped string operands the result is a non-constant value of type string. (…)
Why not make it constant? After all, len(multiline)
is constant.
There are other solutions to this. One could just use multiline[1:]
everywhere where one doesn't want the leading newline. Or start "This is a long text" on the first line, but that hurts readability and "editability". Another solution is to make multiline
a variable. The use case is pretty obscure and there isn't an immediate need for this, so this proposal is more about a question, why are slices (and indexing!) of constant strings not constant in the first place?