Skip to content

Instantly share code, notes, and snippets.

@hartfordfive
Created February 4, 2016 14:38
Show Gist options
  • Select an option

  • Save hartfordfive/797c0cf6aaf97dc3609a to your computer and use it in GitHub Desktop.

Select an option

Save hartfordfive/797c0cf6aaf97dc3609a to your computer and use it in GitHub Desktop.
Accessing Go Struct Tags
package main
import (
"fmt"
"reflect"
)
type Person struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Email string `json:"email"`
}
person := Person{}
sr := reflect.TypeOf(person)
// Get the first field in the struct, FirstName
field := sr.Field(0)
// Now get the tag labled 'json'
fmt.Println(field.Tag.Get("json"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment