Skip to content

Instantly share code, notes, and snippets.

@denniswebb
Created October 19, 2017 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denniswebb/29175dc58e5b737a3f8c39a1fd20d380 to your computer and use it in GitHub Desktop.
Save denniswebb/29175dc58e5b737a3f8c39a1fd20d380 to your computer and use it in GitHub Desktop.
package main
import "reflect"
func FieldValueByTag(s interface{}, tag, tagValue string) interface{} {
t := reflect.TypeOf(s)
for i := 0; i < t.NumField(); i++ {
if tagValue == t.Field(i).Tag.Get(tag) {
return reflect.ValueOf(s).FieldByName(t.Field(i).Name)
}
}
return nil
}
@denniswebb
Copy link
Author

Searches a struct for a field matching a tag/value pair and returns the value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment