Skip to content

Instantly share code, notes, and snippets.

@Developer1Dev
Forked from unwosu/populate.go
Created June 1, 2020 11:48
Show Gist options
  • Save Developer1Dev/5ef4992014139aa0d0906c2f77e1d53b to your computer and use it in GitHub Desktop.
Save Developer1Dev/5ef4992014139aa0d0906c2f77e1d53b to your computer and use it in GitHub Desktop.
func populate(v reflect.Value, value string) error {
switch v.Kind() {
case reflect.String:
v.SetString(value)
case reflect.Int:
i, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return err
}
v.SetInt(i)
case reflect.Bool:
b, err := strconv.ParseBool(value)
if err != nil {
return err
}
v.SetBool(b)
default:
return fmt.Errorf("unsupported king %s", v.Type())
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment