Golang bug or intended feature on map literals?

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.


Go has semicolons, but you don't see them because they're inserted automatically by the lexer.

Semicolon insertion rules:

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.

Tags:

Go