Golang utility to transform a struct to a map with keys in lowercase. Useful to map struct to json (uppercase names always make me blink).
// see https://play.golang.org/p/6YD4YdvGLr
// for an example
import (
"reflect"
"unicode"
"unicode/utf8"
)
func structToLowerFirstMap(in interface{}) map[string]interface{} {
v := reflect.ValueOf(in)
vType := v.Type()
result := make(map[string]interface{}, v.NumField())
for i := 0; i < v.NumField(); i++ {
name := vType.Field(i).Name
result[lowerFirst(name)] = v.Field(i).Interface()
}
return result;
}
func lowerFirst(s string) string {
if s == "" {
return ""
}
r, n := utf8.DecodeRuneInString(s)
return string(unicode.ToLower(r)) + s[n:]
}