Most confusing compiler error in Go?

2,720 views
Skip to first unread message

Steven Rice

unread,
Sep 14, 2012, 6:34:09 PM9/14/12
to golan...@googlegroups.com
cannot use new(int) (type *int) as type *interface {} in return argument:
*interface {} is pointer to interface, not interface


I know what I did to get the message, I am not sure I understand why I got it though. And the last part of the error is certainly misleading. I thought it was comical thoguh.

Kyle Lemons

unread,
Sep 14, 2012, 6:59:38 PM9/14/12
to Steven Rice, golan...@googlegroups.com
The first part is the relevant error (basically saying *int != *interface{}), I think the second is trying to be helpful and will be more or less so depending on the particular instance.  If you have a type FooBar interface{...}, then the "*FooBar is pointer to interface, not interface" would be more enligtening.

--
 
 

Russ Cox

unread,
Sep 17, 2012, 3:11:21 PM9/17/12
to Steven Rice, golan...@googlegroups.com
What part is misleading? If you had mistyped interface{} as
*interface{} (or io.Reader as *io.Reader), which is a common mistake
among new Go programmers, the error makes clear that the pointer is
the issue. If you'd used plain interface{} as the return type, the
return statement would be legal.

Russ

Steven Rice

unread,
Sep 17, 2012, 4:53:47 PM9/17/12
to golan...@googlegroups.com, Steven Rice
The part in which I was confused is that *T is interface{} and not *{}interface, especially since once you return a *T as {}interface you get an invalid indirect error for doing this:

*returnedValue = 9

Steven Rice

unread,
Sep 17, 2012, 4:54:01 PM9/17/12
to golan...@googlegroups.com, Steven Rice
The part in which I was confused is that *T is interface{} and not *{}interface, especially since once you return a *T as {}interface you get an invalid indirect error for doing this:

*returnedValue = 9

On Monday, September 17, 2012 2:11:26 PM UTC-5, Russ Cox wrote:

Jesse McNelis

unread,
Sep 17, 2012, 7:29:09 PM9/17/12
to Steven Rice, golan...@googlegroups.com
On Tue, Sep 18, 2012 at 6:53 AM, Steven Rice <stevenw...@gmail.com> wrote:
> The part in which I was confused is that *T is interface{} and not
> *{}interface, especially since once you return a *T as {}interface you get
> an invalid indirect error for doing this:
>
> *returnedValue = 9

It's a fairly common confusion of new go devs to think that
interface{} is the same as 'void*' in C or 'object' in Java.
Interfaces in Go are distinct values from the values they contain.
Often knowing the implementation details clears this up.
http://research.swtch.com/interfaces

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

Rob Pike

unread,
Sep 17, 2012, 7:41:20 PM9/17/12
to Jesse McNelis, Steven Rice, golan...@googlegroups.com
http://golang.org/2011/09/laws-of-reflection.html has a much shorter
discussion that should be sufficient for most people.

-rob

Rob Pike

unread,
Sep 17, 2012, 7:41:51 PM9/17/12
to Jesse McNelis, Steven Rice, golan...@googlegroups.com
Except that I meant this: http://golang.org/doc/articles/laws_of_reflection.html

Sorry.

-rob
Reply all
Reply to author
Forward
0 new messages