mercredi 19 juin 2019

How to get field's tag without using the field name as string?

Is it possible to fetch a field tag using a function that receives only the struct and the field itself ?

I know that I can do a thing like this:

reflect.TypeOf(x).FieldByName("FieldNameAsString").Tag

But I don't want to use the field's name as string in this case because it could be renamed in the future, so it is better to use the field itself instead.

type MyStruct struct {
    MyField string `thetag:"hello"`
}

func main() {
    x := MyStruct{}
    getTag(x, x.MyField)
}





Aucun commentaire:

Enregistrer un commentaire