I want to get the name of a field name by using reflection. I pass the string as a pointer and then I want to retrieve the variable name in the function.
type FooBar struct {
foo *string
}
func bar(s *string) {
var name string
// TODO: Get name of the field that s is pointing to!
fmt.Println("Expected string is foo: " + name)
}
func main() {
f := Foo{"bar"}
bar(f.s)
}
I tried getting the value of s with
val := reflect.ValueOf(s)
and then I am out of my depth to get the variable name.
I should mention that there are multiple fields in my struct and I don't know in advance which field is in question.
Any help is appreciated. Thanks
Aucun commentaire:
Enregistrer un commentaire