Confused about "syntax error: unexpected comma, expecting {" with multiple outputs

1,944 views
Skip to first unread message

Brendan Tracey

unread,
Feb 1, 2013, 3:27:33 PM2/1/13
to golan...@googlegroups.com
I don't understand why f2 compiles but f1 does not. Help understanding this error would be appreciated

http://play.golang.org/p/S4n6Nff1W9

Scott Lawrence

unread,
Feb 1, 2013, 3:28:41 PM2/1/13
to Brendan Tracey, golan...@googlegroups.com
On Fri, 1 Feb 2013, Brendan Tracey wrote:

> I don't understand why f2 compiles but f1 does not. Help understanding this
> error would be appreciated

Because []Datapoint is a type, not a value. You want

return []Datapoint{}, nil

When you write 'var a []Datapoint', that's equiv. to 'var a := []Datapoint{}'.

>
> http://play.golang.org/p/S4n6Nff1W9
>
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

--
Scott Lawrence

go version go1.0.3
Linux baidar 3.7.4-1-ARCH #1 SMP PREEMPT Mon Jan 21 23:05:29 CET 2013 x86_64 GNU/Linux

Dmitri Shuralyov

unread,
Feb 1, 2013, 3:37:21 PM2/1/13
to golan...@googlegroups.com
If, instead of using var blah = []Datapoint you did blah := []Datapoint, you would get the same error.

You want to write []Datapoint{} instead.

Interestingly, the error description literally tells you how to fix the problem.

Brendan Tracey

unread,
Feb 1, 2013, 3:51:01 PM2/1/13
to golan...@googlegroups.com
Thanks for the help. It does literally tell me how to fix the problem, though not really the source of the problem (which is that I was returning a value, not a type). I was confused about it telling me that there is a syntax error. In other languages I've used, "unexpected comma, expecting {" would generally mean I had forgotten an open brace. I'm not complaining about the error, from the compiler's perspective it is a syntax error.
Reply all
Reply to author
Forward
0 new messages