Created
October 19, 2017 15:48
-
-
Save denniswebb/29175dc58e5b737a3f8c39a1fd20d380 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
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Searches a struct for a field matching a tag/value pair and returns the value.