This is a little weird

1,750 views
Skip to first unread message

Bob Hutchison

unread,
Jun 30, 2012, 3:58:23 PM6/30/12
to golang-nuts
Hi,

There's some code simple code here (http://play.golang.org/p/xvaJAtVamM) that compiles and runs. If you remove the comma on line 21 you get a compiler error and what I find an incomprehensible error message:

prog.go:21: syntax error: unexpected semicolon or newline, expecting }
prog.go:23: non-declaration statement outside function body
prog.go:24: non-declaration statement outside function body
prog.go:25: non-declaration statement outside function body
prog.go:26: syntax error: unexpected }

Really? Is that what's meant to happen? Surely not.

Honestly, I'd have thought the extraneous comma would have been the problem not the solution.

Cheers,
Bob

Bob Hutchison

unread,
Jun 30, 2012, 4:01:49 PM6/30/12
to Bob Hutchison, golang-nuts
If you have lines 19 through 22 on a single line the trailing comma isn't necessary (works with it and without it)

>
> Cheers,
> Bob

Julien Laffaye

unread,
Jun 30, 2012, 4:03:58 PM6/30/12
to golan...@googlegroups.com
This is because of the automatic semi-colon insertion.
http://play.golang.org/p/U8CTrjMq9n compiles, for instance.

This is also why the curly bracket must be on the same line of an if.

Patrick Mylund Nielsen

unread,
Jun 30, 2012, 4:04:30 PM6/30/12
to Bob Hutchison, golang-nuts
Composite literals require trailing commas before newlines because of how semi-colon insertion works: http://golang.org/ref/spec#Semicolons

When you have the comma, no semicolon is inserted, and it continues to the closing bracket.

Aram Hăvărneanu

unread,
Jun 30, 2012, 4:06:24 PM6/30/12
to Patrick Mylund Nielsen, Bob Hutchison, golang-nuts
This is a great feature because it makes it easier to move lines
around. No need to add and remove trailing commas.

--
Aram Hăvărneanu

Patrick Mylund Nielsen

unread,
Jun 30, 2012, 4:12:59 PM6/30/12
to Aram Hăvărneanu, Bob Hutchison, golang-nuts
Yep. I mess up trailing commas in most other languages all the time now, but I'm not sorry.

David Klaffenbach

unread,
Jun 30, 2012, 4:14:50 PM6/30/12
to golan...@googlegroups.com
Or you can go fmt and get: prog.go:21:40: missing ',' before newline in composite literal
Seems clear enough.

hutch

unread,
Jun 30, 2012, 4:22:03 PM6/30/12
to golan...@googlegroups.com
Okay, thanks... Maybe that is the reason, but...

What does the fourth clause of point 1 mean?

The error message still sucks.

hutch

unread,
Jun 30, 2012, 4:23:23 PM6/30/12
to golan...@googlegroups.com
Never mind.

hutch

unread,
Jun 30, 2012, 4:25:05 PM6/30/12
to golan...@googlegroups.com
It *never* would have occurred to me to use go fmt to get a usable error message.

hutch

unread,
Jun 30, 2012, 4:36:28 PM6/30/12
to golan...@googlegroups.com
So we turn commas into terminators because semicolons are separators. For 35 years I've listened to and even participated in this separator vs. terminator debate. I think it's time to just admit Lisp got it right 55 years ago. Sigh.

Well. Carry on!

David Klaffenbach

unread,
Jun 30, 2012, 4:42:37 PM6/30/12
to golan...@googlegroups.com
That's interesting.  If I make a bunch of changes doing go fmt catches some of my typos (like if I slip up and use python syntax somewhere) so I've fallen into the habit of using it before building.

David Klaffenbach

unread,
Jun 30, 2012, 4:43:46 PM6/30/12
to golan...@googlegroups.com
But I agree it does seem odd or at least inconsistent that go fmt identifies an error that the compiler doesn't.


On Saturday, June 30, 2012 3:25:05 PM UTC-5, hutch wrote:

Patrick Mylund Nielsen

unread,
Jun 30, 2012, 4:48:00 PM6/30/12
to David Klaffenbach, golan...@googlegroups.com
As far as I can tell, it has been fixed. Compiling http://play.golang.org/p/q1GuJd-5lZ at tip gives:

./main.go:11: syntax error: need trailing comma before newline in composite literal
./main.go:13: non-declaration statement outside function body
./main.go:14: syntax error: unexpected }

David Klaffenbach

unread,
Jun 30, 2012, 5:29:04 PM6/30/12
to golan...@googlegroups.com, David Klaffenbach
I'm seeing a pattern in the postings in golang-nuts.  It goes something like this:
1)  This is weird.
2)  Yes it is.
3)  Hey, it's fixed now.

Good job go authors!


On Saturday, June 30, 2012 3:48:00 PM UTC-5, Patrick Mylund Nielsen wrote:
As far as I can tell, it has been fixed. Compiling http://play.golang.org/p/q1GuJd-5lZ at tip gives:

./main.go:11: syntax error: need trailing comma before newline in composite literal
./main.go:13: non-declaration statement outside function body
./main.go:14: syntax error: unexpected }

Jesse McNelis

unread,
Jul 1, 2012, 10:57:59 AM7/1/12
to David Klaffenbach, golan...@googlegroups.com
On Sun, Jul 1, 2012 at 7:29 AM, David Klaffenbach <da...@klaff.org> wrote:
> I'm seeing a pattern in the postings in golang-nuts. It goes something like
> this:
> 1) This is weird.
> 2) Yes it is.
> 3) Hey, it's fixed now.
>
> Good job go authors!

The Go devs have time machines. As soon as you report bugs they go
back in time and fix them. This is also why 'Russ Cox' is so many
people (and thus so productive). Every time he goes back in time
another copy of him is created.

--
=====================
http://jessta.id.au

Jan Mercl

unread,
Jul 1, 2012, 3:59:07 PM7/1/12
to Jesse McNelis, golan...@googlegroups.com, David Klaffenbach

Dne 1.7.2012 16:58 "Jesse McNelis" <jes...@jessta.id.au> napsal(a):


>
> The Go devs have time machines. As soon as you report bugs they go
> back in time and fix them. This is also why 'Russ Cox' is so many
> people (and thus so productive). Every time he goes back in time
> another copy of him is created.

You dont't simply leak such info. Now there's a Googler going after your 10yo grandfather.

-j

Sameer Ajmani

unread,
Jul 1, 2012, 4:35:52 PM7/1/12
to Jan Mercl, golang-nuts, Jesse McNelis, David Klaffenbach

Won't work. Russ is his own grandfather.

Patrick Mylund Nielsen

unread,
Jul 1, 2012, 4:36:55 PM7/1/12
to Sameer Ajmani, Jan Mercl, golang-nuts, Jesse McNelis, David Klaffenbach
This thread just got weiiiird.

Sameer Ajmani

unread,
Jul 1, 2012, 4:44:49 PM7/1/12
to Patrick Mylund Nielsen, golang-nuts, Jan Mercl, Jesse McNelis, David Klaffenbach

We went back in time and changed the subject of this thread accordingly.

Patrick Mylund Nielsen

unread,
Jul 1, 2012, 5:02:02 PM7/1/12
to Sameer Ajmani, golang-nuts, Jan Mercl, Jesse McNelis, David Klaffenbach

Matthew White

unread,
Jul 1, 2012, 8:30:58 PM7/1/12
to Sameer Ajmani, Jan Mercl, golang-nuts, Jesse McNelis, David Klaffenbach
Inline image 1
Reply all
Reply to author
Forward
0 new messages