jeudi 21 novembre 2019

Can I set tag dynamically on runtime in golang?

I want to set tag dynamically in struct fields in golang.

For example, I have a struct like this,

type user struct {
  Name  string  
  Email  string `valid:"MaxSize(1)"` characters.
}

Now during execution, I want to set same tags for name field as well.

I am trying something like this,

    switch {
    case isStruct(objT):
    case isStructPtr(objT):
        objT = objT.Elem()
    default:
        //err = fmt.Errorf("%v must be a struct or a struct pointer", model)
        return
    }

    for i := 0; i < objT.NumField(); i++ {
        // ... and start using structtag by parsing the tag
        fmt.Println(objT.Field(i))
        objT.Field(i).Tag = `valid:"MaxSize(1)"`
    }

But I am unable to assign tag like this. can Somebody help?





Aucun commentaire:

Enregistrer un commentaire