Check it:
func main() {
type Something struct {
Some string
Thing int
}
var props []string
var vals []string
m := Something{"smth", 123}
s := reflect.ValueOf(m)
for i := 0; i < s.NumField(); i++ {
props = append(props, s.Type().Field(i).Name)
vals = append(vals, fmt.Sprintf("%v", s.Field(i).Interface()))
}
fmt.Println(props)
fmt.Println(vals)
}
Result:
[Some Thing]
[smth 123]
It also works with named fields like:
Something{
Some: "smth",
Thing: 123,
}
Also as you need to an array (actually slice!) of strings (even if your values are in another type like int), You can use fmt.Sprintf()
to convert interface{}
to string
solved Build arrays out of struct