Good day,
Studying Go, trying to figure out reflection
There is an example:
package main import ( "flag" "fmt" "reflect" ) type AppConfig struct { Pg string `cli:"pg" env:"PG" default:"host=host.local dbname=db user=user password=password" description:"Connection to PostgreSQL"` } func main() { config := &AppConfig{} GetConfig(config) fmt.Println(config.Pg) } func GetConfig(config interface{}) interface{} { e := reflect.ValueOf(config).Elem() t := e.Type() flag.StringVar( e.Field(0).Interface().(*string), t.Field(0).Tag.Get("cli"), t.Field(0).Tag.Get("default"), t.Field(0).Tag.Get("description")) flag.Parse() return config } Link to the playground https://play.golang.org/p/KmQDYJaizl
When starting, I expect to receive:
panic: interface conversion: interface {} is string, not *string If we replace e.Field(0).Interface().(*string) on the correct one (from my point of view) &e.Field(0).Interface().(string) I get:
main.go:22: cannot take the address of e.Field(0).Interface().(string) Actually the question is what is not understood correctly? Or what am I doing wrong?