Accessing an anonymous struct or, "missing type in composite literal"

2,189 views
Skip to first unread message

Torrance Hodgson

unread,
Sep 14, 2013, 1:14:32 AM9/14/13
to golan...@googlegroups.com
Hi all,

I have a struct type that contains an anonmous struct within it. See http://play.golang.org/p/sjuv7krgp6

I'd like to be able to instantiate this struct, but I'm unsure how to do this. At the moment I'm getting a "missing type in composite literal" error — but short of creating an additional type, I'm not sure how to name this anonymous struct.

Is what I'm trying to do possible?

Torrance

Dave Cheney

unread,
Sep 14, 2013, 1:22:22 AM9/14/13
to Torrance Hodgson, golang-nuts
A possible solution,

http://play.golang.org/p/vLJRxnuAKz
> --
> 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.

Torrance Hodgson

unread,
Sep 14, 2013, 1:25:53 AM9/14/13
to golan...@googlegroups.com, Torrance Hodgson
Thanks - that's the solution I'd just come to. It's a bit verbose and will soon get very non-DRY, so I might as well name the inner struct.

Torrance

Dave Cheney

unread,
Sep 14, 2013, 1:33:27 AM9/14/13
to Torrance Hodgson, golang-nuts
Yeah, every time I have tried this, usually in a test, the verbosity
quickly drives me towards declaring a new struct. It can be done with
inline anon declarations, but the readability hit rarely justifies the
cleverness.
Reply all
Reply to author
Forward
0 new messages