Need trailing comma before newline in composite literal

3,494 views
Skip to first unread message

r2p2

unread,
Nov 16, 2011, 4:01:40 PM11/16/11
to golan...@googlegroups.com
Hi,

after one year, I try to relearn go. (Thanks to the two years birthday news everywhere.)
I wanted to start with a reimplement his ( http://wh3rd.net/practical-go/#(1) ) url shortener. But on page 12 ( http://wh3rd.net/practical-go/#(12) ) I stumbled over weird syntax.

For me it is not logical why I have to set there a comma. Is there a deeper reason?



With kind regards
r2p2

Evan Shaw

unread,
Nov 16, 2011, 5:45:55 PM11/16/11
to golan...@googlegroups.com
On Thu, Nov 17, 2011 at 10:01 AM, r2p2 <robert...@gmx.net> wrote:
> For me it is not logical why I have to set there a comma. Is there a deeper
> reason?

Yes, it's because of automatic semicolon insertion. Semicolons are
automatically inserted at the end of a line in many situations,
including when the last token is a parenthesis. (See
http://golang.org/doc/go_spec.html#Semicolons)

The comma tells the compiler "don't insert a semicolon here; this
isn't the end of a statement."

- Evan

Andrew Gerrand

unread,
Nov 17, 2011, 11:17:18 PM11/17/11
to golan...@googlegroups.com
It's not just that. It also means that you can more easily re-order the lines of a literal without needing to worry about adding or removing commas. (As is the case with JavaScript, for example.)

Andrew

r2p2

unread,
Nov 24, 2011, 8:51:48 AM11/24/11
to golan...@googlegroups.com
Well, this is a good point. But it still doesn't look natural.

John Asmuth

unread,
Nov 24, 2011, 9:58:37 AM11/24/11
to golan...@googlegroups.com
Source code of any language doesn't look natural until you've been staring at it for a while.

Since I've been writing Go for more than a year, the comma at the end looks like the most natural thing in the world :)

Bobby Powers

unread,
Nov 24, 2011, 10:06:54 AM11/24/11
to golan...@googlegroups.com
On Fri, Nov 18, 2011 at 5:17 AM, Andrew Gerrand <a...@golang.org> wrote:
It's not just that. It also means that you can more easily re-order the lines of a literal without needing to worry about adding or removing commas. (As is the case with JavaScript, for example.)

It also makes version control diffs nicer when adding items to lists/structure initializations, etc.  It gets rid of the:

-     someItemThatHasntActuallyChanged
+     someItemThatHasntActuallyChanged,

noise, which makes me happy.

yours,
Bobby

pg...@redhat.com

unread,
Aug 12, 2016, 9:55:43 AM8/12/16
to golang-nuts
Because of this reason I started feeling it natural :D thanks Andrew Gerrand for pointing out.
Reply all
Reply to author
Forward
0 new messages