Arrays of structures

I threw some code together and surprisingly it works. Sort of. I assign some variables to an array. Using the array name, I can access the ‘name’ field of variable, but not the ‘present’ field. I suspect I am doing something illegal, but why does it work at all?

http://play.golang.org/p/5OCa7kTRq4

It’s because your array is holding a copy of the animals Rabbit, Chicken… see here for a working example http://play.golang.org/p/lpWBlzjlTR

Could have also updated by accessing this index and assigning that way.

I see that you used pointers/ addresses to make it work. But I still don’t see how my original managed to print the name.
The addresses of animals[0].name and Rabbit.name are different, so I’m thinking the assignment copied the name from one structure to another. Doh! Array animals got a copy of data structures. Changing the data of an individual animal (like Rabbit) doesn’t change the data in animals.

Thanks for helping me clarify my thinking.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.