I would like to manipulate structs at Runtime.
For example, I have a struct:
type Item struct {
SomeField string
}
Is it possible to add field on runtime? Or Access Attribute that is not yet defined. Something like pythons __getattr__() or __call__(
) so I could dynamically control the fields/methods accessed.
E.g. do something like Item.DynamicField or Item.DynamicMethod()
where I don't know exactly the Field or the Method that will be accessed/called, So I can't define it statically.
Maybe I'm missing something in the Reflect package?
Thank you.
https://github.com/oleiade/reflections
The purpose of reflections package is to make developers life easier when it comes to introspect structures at runtime. Its API is inspired from python language (
getattr
,setattr
,hasattr
...) and provides a simplified access to structure fields and tags.
Is it possible to add field on runtime? Or Access Attribute that is not yet defined.
No. Go is a compiled language with statically defined types. You probably need a map if you want to dynamically add properties.