Recursive struct types

March 21, 2023

Finally, after more than a week, we’re at the end of our discussion of Go’s struct types.

You may recall that recursion is not permitted within array types. We have a similar, though not identical, restriction for structs:

Struct types

A struct type T may not contain a field of type T, or of a type containing T as a component, directly or indirectly, if those containing types are only array or struct types.

// invalid struct types
type (
	T1 struct{ T1 }            // T1 contains a field of T1
	T2 struct{ f [10]T2 }      // T2 contains T2 as component of an array
	T3 struct{ T4 }            // T3 contains T3 as component of an array in struct T4
	T4 struct{ f [10]T3 }      // T4 contains T4 as component of struct T3 in an array

// valid struct types
type (
	T5 struct{ f *T5 }         // T5 contains T5 as component of a pointer
	T6 struct{ f func() T6 }   // T6 contains T6 as component of a function type
	T7 struct{ f [10][]T7 }    // T7 contains T7 as component of a slice in an array

The fact that a struct may contain a pointer to itself is especially powerful, because it makes it possible to represent certain types of trees as Go structs. A great example would be the x/net/html.Node type, which contians a number of fields of type *Node.

Quotes from The Go Programming Language Specification Version of December 15, 2022

Share this