Skip to content

Instantly share code, notes, and snippets.

@enjoylife
Created September 8, 2018 21:20
Show Gist options
  • Save enjoylife/92598ea72768d34645169d3d6d9dda0d to your computer and use it in GitHub Desktop.
Save enjoylife/92598ea72768d34645169d3d6d9dda0d to your computer and use it in GitHub Desktop.
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