types lookup for error interface type

71 views
Skip to first unread message

Dan Kortschak

unread,
Dec 14, 2019, 4:07:50 AM12/14/19
to golang-nuts
In the go/types package there is an easy way to obtain a types.Type for
basic builtin types. This doesn't exist for the error type.

Is there an easier way to get a types.Type representing error than by
using NewInterfaceType, NewFunc and NewSignature functions (I am
assuming that the nil *types.Package is what should be used for the
call to NewFunc).

thanks
Dan

Marcin Romaszewicz

unread,
Dec 14, 2019, 12:15:48 PM12/14/19
to Dan Kortschak, golang-nuts
Error is an interface, so its type depends on the implementation satisfying that interface. You can't just create an "error", since they don't exist, all that exists are implementation satisfying the error interface. That's what you're doing with NewSignature.

Could you just create an implementation of the error interface and instantiate one of those?





--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/4fb681518e2e36e0d0564394546dd7879dd8f96a.camel%40kortschak.io.

Axel Wagner

unread,
Dec 14, 2019, 12:55:19 PM12/14/19
to Dan Kortschak, golang-nuts

Axel Wagner

unread,
Dec 14, 2019, 12:55:35 PM12/14/19
to Dan Kortschak, golang-nuts
whoops, I misread :) types.Type,, not reflect :)

Dan Kortschak

unread,
Dec 14, 2019, 2:13:52 PM12/14/19
to golang-nuts
I think I must have asked this really badly.

I don't want a reflect.Type of an error, I want a types.Type
representation of error for performing static analysis.

The playground that I imagine is necessary is something like this.

https://play.golang.org/p/e-FU0_4J9jK

This seem massively over complicated, so I'm wondering if there is an
easier way.

Paul Jolly

unread,
Dec 14, 2019, 2:20:39 PM12/14/19
to Dan Kortschak, golang-nuts
> This seem massively over complicated, so I'm wondering if there is an
> easier way.

I think you're after types.Universe.Lookup("error")?


Paul

Dan Kortschak

unread,
Dec 14, 2019, 2:51:13 PM12/14/19
to Paul Jolly, golang-nuts
I think you are right. Thanks.
Reply all
Reply to author
Forward
0 new messages