I have some special type in Golang what represent a string with Validate method.
type string128 string
func (s *string128) Validate() error {
...
return nil
}
There is structs have fields like the following:
type Strings1 struct {
str1 string
str2 string128
str3 string128
...
strN+1 string128
strN+2 string
}
type Strings2 struct {
str1 string
str2 string128
str3 string128
...
strN+1 string128
strN+2 string
}
I want to make a function where I can pass them and if the field have a Validate() function call it.
What I have done:
func validateStruct(a interface{}) error {
v := reflect.ValueOf(a)
t := reflect.TypeOf(a)
for i := 0; i < v.NumField(); i++ {
err := reflect.TypeOf(v.Field(i).Interface()).MethodByName("Validate").Call(...)
}
return nil
}
But it is do a lots of panic.
Aucun commentaire:
Enregistrer un commentaire