Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang bug or intended feature on map literals?

Tags:

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

like image 722
BojanT Avatar asked Mar 27 '15 12:03

BojanT


1 Answers

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.

like image 189
thwd Avatar answered Oct 04 '22 06:10

thwd