unsafe: document a difference in behavior between unsafe.Sizeof(x) and reflect.TypeOf(x).Size() #67465
Labels
compiler/runtime
Issues related to the Go compiler and/or runtime.
Documentation
FixPending
Issues that have a fix which has not yet been reviewed or submitted.
NeedsInvestigation
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone
Go version
go version go1.22.3 darwin/arm64
Output of
go env
in your module/workspace:What did you do?
go doc unsafe.Sizeof
andgo doc reflect.Type
What did you see happen?
Per https://go.dev/play/p/ovkwhK0eSWV for a variable of interface static type:
unsafe.Sizeof()
returns the size of the static type of the interface (which is 2*WordCount
= 16 on most architectures)reflect.TypeOf().Size()
returns the size of the dynamic type of the interface (which depends on the number of fields and underlying padding of the struct implementing that interface)What did you expect to see?
A statement either in
go doc unsafe.Sizeof
or ingo doc reflect.Type
that explains the difference in behavior between both functions for variables of interface static type.As a matter of fact
go doc reflect.Type
mentions:Which can be slightly misleading, as
unsafe.Sizeof(i)
will return a different result thanreflect.TypeOf(i).Size()
ifi
is of interface static type (asreflect.TypeOf()
returns the dynamic type of the interface as already documented ingo doc reflect.TypeOf
)I've submitted the following CL as a documentation suggestion: https://go-review.googlesource.com/c/go/+/586275
The text was updated successfully, but these errors were encountered: