Today, our final note on array types, and when recursion of types is, and is not, permitted:
Array types
…
An array type
T
may not have an element of typeT
, or of a type containingT
as a component, directly or indirectly, if those containing types are only array or struct types.// invalid array types type ( T1 [10]T1 // element type of T1 is T1 T2 [10]struct{ f T2 } // T2 contains T2 as component of a struct T3 [10]T4 // T3 contains T3 as component of a struct in T4 T4 struct{ f T3 } // T4 contains T4 as component of array T3 in a struct ) // valid array types type ( T5 [10]*T5 // T5 contains T5 as component of a pointer T6 [10]func() T6 // T6 contains T6 as component of a function type T7 [10]struct{ f []T7 } // T7 contains T7 as component of a slice in a struct )
I think the explanation and provided examples are pretty clear. If you ever find yourself wanting this type of recursion, you’ll need to introduce some new intermediate type, such as an empty interface.
type T1 [10]any // Now the elements of T1 may be of type `T1` via the empty
// interface. However, they can also be of any other type, so
// probably not a great solution for most uses.
Quotes from The Go Programming Language Specification, Version of January 19, 2023