Jay Taylor's notes
back to listing indexGolang - Get pointer to value using reflection
[web search]
I have a function that iterates through all fields of an interface passed as parameter. In order to achieve this is I am using reflection. The issue is that I do not know how to obtain the address of a non-pointer field. Here is an example:
The above code represents my test structures. Now here is the actual function which traverses a specified structure and lists details about it:
And here is the actual test after structure instantiation/initialization:
And finally the output of InspectStruct call:
As you can see I am using recursion, so if one of the fields is a struct kind then I call InspectStruct for it. My issue is that though all fields have been initialized for the entire structure "t" hierarchy, I am not able to get the address for any field located at a higher depth than "t". I would really appreciate any help. |
|||
Passing Working example : http://play.golang.org/p/nleA2YWMj8
|
|||
I was going to leave this as a comment on OneOfOne's post, but it turned out a little too complex for a comment and very relevant to answering the question and why OneOfOne's answer works correctly. The reason
However, think about the As you'll note, passing a value directly to So, in our example above, if we were to naively pass in So why does passing the
All it can give you is this
|
|||||||||||||||
|
Your Answer
asked |
1 year ago |
viewed |
2429 times |
active |
- Gaia GPS - wild team seeks 4th developer, outdoorsy and intelligentGaia GPSWORK REMOTELYjavascriptamazon-web-services
- Senior Server-Side DeveloperThird IronWORK REMOTELYjavascriptamazon-web-services
- Work with the latest tech! We're jetting toward a new cutting edge environment!kCuraWORK REMOTELYjavascriptazure
- Sports/eSports Team Hiring Senior Full StackVantage SportsWORK REMOTELYgodocker
Get the weekly newsletter!
- Top questions and answers
- Important announcements
- Unanswered questions
see an example newsletter
Related
Technology | Life / Arts | Culture / Recreation | Science | Other | ||
---|---|---|---|---|---|---|