I'm working on a go module which is descending down through a hierarchy of types and using reflect. No worries about "sleighting" go's efficiency by using reflect, this is just for part of the initialization – it will never affect on-going operations.
Anyway, I passed a reflect.value
and a reflect.type
type to a function and then was unable to get the methods of the underlying structure. Instead I got the methods of the reflect type. After playing with it for a while I wasn't able to actually get to the underlying structure with functions like x.Method(i)
or x.MethodByName("my-method")
.
So my question is, can you get the underlying methods with just a reflect type or value or do you need the original variable?
So what I mean by types in types is:
type a struct {
name string
something b
}
type b struct {
i int
s string
}
var c a
func (d a) Validate() {
// stuff here
}
Now let's suppose we have something like:
func stuff() {
v := reflect.ValueOf(c)
t := reflect.TypeOf(c)
otherStuff(v, t)
}
func otherStuff(v reflect.Value, t reflect.Type) {
// Can I get the method "Validate" here with just the reflect types or do I need to pass the
// original variable?
// In reality b could contain something that contains something so it's a hierarchy calling for
// some generic code, if at all possible.
}
Aucun commentaire:
Enregistrer un commentaire