-
Notifications
You must be signed in to change notification settings - Fork 17.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
reflect: documentation of StructField.Offset is not clear about type embedding #61495
Comments
|
Both. I just try to use unsafe, reflect and generics together to write a library, it is faster than my manual code to solve the same problem in my benchmark. amazing!!! func SliceToMapFieldAsKey[Key_t comparable,Item_t any](s []Item_t,fieldName string) map[Key_t]Item_t{
var key Key_t
rkeyt:=reflect.TypeOf(key)
m:=make(map[Key_t]Item_t,len(s))
t1:=reflect.TypeOf(s)
t1e:=t1.Elem()
if t1e.Kind()!=reflect.Struct{
panic(`only support slice of struct now.`)
}
f,ok:=t1e.FieldByName(fieldName)
if ok==false{
panic(`SliceToMap1 fieldName not found`)
}
if f.Type.ConvertibleTo(rkeyt)==false{
panic(`SliceToMap1 fieldName type not match`)
}
offset:=mustGetOffsetFormStruct(t1e,fieldName)
for i:=range s{
key=*(*Key_t)(unsafe.Pointer(uintptr(unsafe.Pointer(&s[i]))+offset))
m[key] = s[i]
}
return m
}
func mustGetOffsetFormStruct(rs reflect.Type,fieldName string) uintptr{
f,ok:=rs.FieldByName(fieldName)
if ok==false{
panic(`fieldName not in struct`)
}
if len(f.Index)==1{
return f.Offset
}
offset:=uintptr(0)
for _,index:=range f.Index{
rf:=rs.Field(index)
offset+=rf.Offset
rs = rf.Type
}
return offset
} |
Do you have any suggestions for how to make the comment clearer? It seems to me that the confusion is not so much about |
It may be better to add another func like
I do not think so. I first use the StructField.Index, it tell me the index of embedded level. So I do not think StructField is return a field in an embedded struct |
Wouldn't it be better to have a similar behavior with the Thus:
In this way, if an array contains one element, it's just an offset within the struct, if more - the struct is embedded. And you can leverage or just ignore that info and go with the last element, which is always an offset within the struct. |
I do not think this design is better, it will confuse/surprise me again. |
I don't see any reason to add new fields or functions or methods. All the information anybody might want is available and correct. As far as I can tell, this is a documentation issue that should be addressed by writing better documentation. |
Mention that StructField.Offset can represent the offset of the embedded structure. Fixes golang#61495 Change-Id: I9abaf3613c797006b803dcb1dbee16f25ffb7516
Change https://go.dev/cl/514035 mentions this issue: |
Thanks a lot . |
I don't think we need a common on StructField.Offset. I think the existing comment there is exactly right. The only confusing case is when you look up a field and don't realize that it is a promoted field, and that is specific to the lookup. We don't need any comment on |
When I read the document I think StructField.Offset means the offset of the field of caller struct, not the offset of embeded struct.But it is the offset of embeded struct.
I think the document should be updated to clear it.
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
https://go.dev/play/p/xae9pAtDcY7
What did you expect to see?
8 8
What did you see instead?
8 0
The text was updated successfully, but these errors were encountered: