Use offsets to find the field:
// getTag returns the tag for a field given a pointer to
// a struct and a pointer to the field in that struct.
func getTag(pv interface{}, pf interface{}) reflect.StructTag {
v := reflect.ValueOf(pv)
offset := reflect.ValueOf(pf).Pointer() - v.Pointer()
t := v.Type().Elem()
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
if f.Offset == offset {
return f.Tag
}
}
return ""
}
Run it on the playground.
The code above assumes that the garbage collector does not move the struct between the to calls to Pointer. This assumption is true today, but may not true in the future. Use the unsafe package to make the code safe against future changes to the garbage collector:
// getTag returns the tag for a field with the given offset
// in the struct pointed to by pv.
func getTag(pv interface{}, offset uintptr) reflect.StructTag {
t := reflect.TypeOf(pv).Elem()
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
if f.Offset == offset {
return f.Tag
}
}
return ""
}
Call it like this:
x := MyStruct{}
fmt.Println(getTag(&x, unsafe.Offsetof(x.MyField)))
Run it on the Playground.