如何在不使用字段名称作为字符串的情况下获取字段的标签?
问题描述:
Is it possible to fetch a field tag using a function that receives only the struct and the field itself ?
I know that I can do a thing like this:
reflect.TypeOf(x).FieldByName("FieldNameAsString").Tag
But I don't want to use the field's name as string in this case because it could be renamed in the future, so it is better to use the field itself instead.
type MyStruct struct {
MyField string `thetag:"hello"`
}
func main() {
x := MyStruct{}
getTag(x, x.MyField)
}
是否可以使用仅接收结构和字段本身的函数来获取字段标签? p>
我知道我可以做这样的事情: p>
reflect.TypeOf( x).FieldByName(“ FieldNameAsString”)。Tag
code> pre>
但是在这种情况下,我不想将字段名称用作字符串,因为可以在其中重命名 将来,最好改用字段本身。 p>
type MyStruct struct {
MyField string`thetag:“ 你好“`
}
func main(){
x:= MyStruct {}
getTag(x,x.MyField)
}
code> pre>
div>
答
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 ""
}
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)))