How do I initialise a doubly/deeply nested struct?

7,257 views
Skip to first unread message

uvic...@gmail.com

unread,
Nov 19, 2013, 12:59:01 PM11/19/13
to golan...@googlegroups.com
I know how to initialise this:

type foo struct {
bar struct {
value int
}
}


How would I initialise this:
type tar struct {
foo struct {
bar struct {
value int
}
}
}


I have deeply nested structs as a result of JSON decoding but would like to also create them manually for testing purposes.

Ian Lance Taylor

unread,
Nov 19, 2013, 6:18:45 PM11/19/13
to uvic...@gmail.com, golang-nuts
There is unfortunately no really useful way to write a composite
literal for that kind of type. Easier to initialize it field by
field. Both versions shown here:

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

Ian

Dan Dubois

unread,
Nov 20, 2013, 3:46:17 PM11/20/13
to golan...@googlegroups.com, uvic...@gmail.com
Thanks!

Matt Harden

unread,
Nov 22, 2013, 7:24:13 PM11/22/13
to Dan Dubois, golang-nuts
Another option is to name the inner structs as well, which makes it more succinct.




--
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.

Matt Harden

unread,
Nov 22, 2013, 7:52:23 PM11/22/13
to Dan Dubois, golang-nuts
Oh, and then you can shorten the initialization to this.

f := &tar{foo{bar{2}}}

Which is not too shabby.

Matt Harden

unread,
Nov 22, 2013, 7:55:22 PM11/22/13
to Dan Dubois, golang-nuts
Does anyone know why Go can't or doesn't infer the type of the initializer on struct fields with a definite type?

Ian Lance Taylor

unread,
Nov 22, 2013, 8:05:40 PM11/22/13
to Matt Harden, Dan Dubois, golang-nuts
On Fri, Nov 22, 2013 at 4:55 PM, Matt Harden <matt....@gmail.com> wrote:
> Does anyone know why Go can't or doesn't infer the type of the initializer
> on struct fields with a definite type?

We tried it, and tried turning it on whereever possible, and it made
complex struct composite literals unreadable.

It's doable if someone wants to make a reasoned argument, but that
argument has to include actually showing the changes on real code.

Ian

Matt Harden

unread,
Nov 23, 2013, 10:34:22 PM11/23/13
to Ian Lance Taylor, Dan Dubois, golang-nuts
But just having the ability to infer the type doesn't force the developer to use it. In the case of structs that contain several levels of anonymous structs, an initializer is currently forced to be very repetitive. For the OP's example, which wasn't that complex, here's what you end up with for a shortest possible initializer:

x := &tar{struct{bar struct{value int}}{struct{value int}{2}}}

Notice the unavoidable repetition of struct{value int}. This gets exponentially worse with as the complexity of anonymous structs grows. For example if the tar type were anonymous as well, we would have this:

x := &struct{foo struct{bar struct{value int}}}{struct{bar struct{value int}}{struct{value int}{2}}}

where we have to repeat bar twice and value three times.

Adding just one field to foo makes it look like this:

x := &struct{foo struct{bar struct{value int}}, baz struct{value string}}}}{struct{bar struct{value int}, baz struct{value string}}{struct{value int}{2}, struct{value string}{"abc"}}}}

I think you can see what I mean by exponentially worse.

If type inference applied, this would be possible (for the original example):

x := &tar{foo: {bar: {value: 2}}}

which to me is much more readable. Yes, it could be shortened further to &tar{{{2}}}, which is arguably less clear, but again the programmer is not forced to use this super-short style, whereas they are currently forced to state and re-state the anonymous struct types, which to me is much less readable.

I realize the example is not "real code", but large hierarchies of anonymous structs can be useful in JSON or XML encoding and decoding. Building tests with hardcoded sample data of those types is currently too onerous. Applying type inference would fix that, without breaking any working code.

tjholo...@gmail.com

unread,
Jul 7, 2014, 7:49:58 PM7/7/14
to golan...@googlegroups.com
I have no real comment other than +1 :D 

xingtao zhao

unread,
Jul 7, 2014, 9:42:32 PM7/7/14
to golan...@googlegroups.com, tjholo...@gmail.com
+1
Reply all
Reply to author
Forward
0 new messages