mardi 28 juillet 2015

Reflect: setting a field of a pointer

I'm trying to do something like this:

Define structs with tags named env:

type Env struct {
     Port string `env:"PORT"`
}

Call some function which will get the environment variable names using os.Getenv and put set it in the struct.

Right now, I have this: http://ift.tt/1MTVytx

But, as you can see, I have to pass both the reference and the pointer to my function.

While this works, it is very ugly (at least I think it is).

If I try to pass the pointer only, I can't get the type right (because it will be an *interface{}) and, if I pass only the reference, I can't set the values using reflect (even if I could, it would not work).

Is there a sane way of doing this?





Aucun commentaire:

Enregistrer un commentaire