Created
September 8, 2018 21:20
-
-
Save enjoylife/92598ea72768d34645169d3d6d9dda0d to your computer and use it in GitHub Desktop.
This file contains 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
func GetJSONFieldName(i interface{}, goFieldName string) string { | |
field, ok := reflect.TypeOf(i).Elem().FieldByName(goFieldName) | |
if !ok { | |
return "" | |
} | |
tag, ok := field.Tag.Lookup("json") | |
if !ok { | |
return "" | |
} | |
commaIndex := strings.Index(tag, ",") | |
jsonName := tag[:commaIndex] | |
return jsonName | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment