First, I would say that it is more idiomatic to define a type for your structure, regardless of how simple the structure is. For example:
type MyStruct struct { MyField int }
This would mean changing the Result structure as follows:
type Result struct { name string Objects []MyStruct }
The reason for your program panic is that you are trying to access an area in memory (an element in the Object array) that has not yet been allocated.
For arrays of structures, this must be done using make .
r.Objects = make([]MyStruct, 0)
Then, to add to your array safely, you better create an instance of MyStruct , i.e.
ms := MyStruct{ MyField: 10, }
And then append enter this into your r.Objects array
r.Objects = append(r.Objects, ms)
For more information on make see the docs .
mattsch
source share