Reputation: 801
Just started to learn Go and I need map of string string, that I initialize literally.
mapa := map[string]string{
"jedan":"one",
"dva":"two"
}
But compiler is complaining syntax error: need trailing comma before newline in composite literal
So I had to add coma after "two",
or delete a new line and have }
after last value for compiler to be happy
Is this intended behavior of code style?
EDIT: to be clear follwing will compile and work
mapa := map[string]string{
"jedan":"one",
"dva":"two" }
go version go1.4.2 darwin/amd64
Mac OSX 10.9.5
Upvotes: 26
Views: 16190
Reputation: 24808
Go has semicolons, but you don't see them because they're inserted automatically by the lexer.
a semicolon is automatically inserted into the token stream at the end of a non-blank line if the line's final token is
- an integer, floating-point, imaginary, rune, or string literal
So this:
mapa := map[string]string{
"jedan": "one",
"dva": "two"
}
is actually:
mapa := map[string]string{
"jedan": "one",
"dva": "two"; // <- semicolon
}
Which is invalid Go.
Upvotes: 49
Reputation: 9509
Yes it is. And you should choose the added comma.
It is much more simple to edit map/slice literals that way : you can copy-paster, move items around without worrying about the fact that the last item shouldn't be followed by a comma.
In fact, you can also do the same in PHP, javascript, and many other languages.
Upvotes: 16