If you’re looking for something like person[property]
like you do in Python or JavaScript, the answer is NO, Golang does not support dynamic field/method selection at runtime.
But you can do it with reflect
:
import (
"fmt"
"reflect"
)
func main() {
type person struct {
name string
age int
}
v := reflect.ValueOf(person{"Golang", 10})
property := "age"
f := v.FieldByName(property)
fmt.Printf("Person Age: %d\n", f.Int())
}
solved How to access to a struct parameter value from a variable in Golang