Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to set value array of struct with pointer in Go

I have an array of Struct that contains a pointer, i want to set this with some data but when it exit the for loop the value will return nil

heres example :
https://go.dev/play/p/iCiHsVfJkMx

Is there any way to return with complete data, I mean with value on it

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You are ranging over t which is of type []Data. Data is not a pointer type, so v will be set to a copy of t[0], then t[1], etc.

You need to access the struct via array index such that there is an intact chain of references.

https://go.dev/play/p/BqgavPfx16V

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading