Skip to content

Instantly share code, notes, and snippets.

@eraclitux
Last active October 13, 2015 08:33
Show Gist options
  • Select an option

  • Save eraclitux/36d30219f38c874968d6 to your computer and use it in GitHub Desktop.

Select an option

Save eraclitux/36d30219f38c874968d6 to your computer and use it in GitHub Desktop.
lower case all struct's string fields in Go
// LowerStruct changes to lower case all struct's string fields
// of the passed struct.
func LowerStruct(strPtr interface{}) error {
str := reflect.ValueOf(strPtr)
if str.Kind() != reflect.Ptr {
return errors.New("pointer to struct expected")
}
v := str.Elem()
for i := 0; i < v.NumField(); i++ {
fieldValue := v.Field(i)
if fieldValue.CanSet() && fieldValue.Kind() == reflect.String {
s := fieldValue.String()
fieldValue.SetString(strings.ToLower(s))
}
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment