Last active
October 13, 2015 08:33
-
-
Save eraclitux/36d30219f38c874968d6 to your computer and use it in GitHub Desktop.
lower case all struct's string fields in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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