Skip to content

unsafe: document a difference in behavior between unsafe.Sizeof(x) and reflect.TypeOf(x).Size() #67465

@Gandem

Description

@Gandem

Go version

go version go1.22.3 darwin/arm64

Output of go env in your module/workspace:

```shell GO111MODULE='on' GOARCH='arm64' GOBIN='' GOCACHE='/Users/nayef.ghattas/Library/Caches/go-build' GOENV='/Users/nayef.ghattas/Library/Application Support/go/env' GOEXE='' GOEXPERIMENT='' GOFLAGS='' GOHOSTARCH='arm64' GOHOSTOS='darwin' GOINSECURE='' GOMODCACHE='/Users/nayef.ghattas/go/pkg/mod' GONOPROXY='' GOOS='darwin' GOPATH='/Users/nayef.ghattas/go' GOPRIVATE='' GOPROXY='https://proxy.golang.org|direct' GOROOT='/opt/homebrew/Cellar/go/1.22.3/libexec' GOSUMDB='sum.golang.org' GOTMPDIR='' GOTOOLCHAIN='auto' GOTOOLDIR='/opt/homebrew/Cellar/go/1.22.3/libexec/pkg/tool/darwin_arm64' GOVCS='' GOVERSION='go1.22.3' GCCGO='gccgo' AR='ar' CC='cc' CXX='c++' CGO_ENABLED='1' GOMOD='/dev/null' GOWORK='' CGO_CFLAGS='-O2 -g' CGO_CPPFLAGS='' CGO_CXXFLAGS='-O2 -g' CGO_FFLAGS='-O2 -g' CGO_LDFLAGS='-O2 -g' PKG_CONFIG='pkg-config' GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/z1/9gbm9wxx6rl1l71csb2w18hc0000gq/T/go-build3531211748=/tmp/go-build -gno-record-gcc-switches -fno-common' ```

What did you do?

go doc unsafe.Sizeof and go 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 in go 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:

	// Size returns the number of bytes needed to store
	// a value of the given type; it is analogous to unsafe.Sizeof.
	Size() uintptr

Which can be slightly misleading, as unsafe.Sizeof(i) will return a different result than reflect.TypeOf(i).Size() if i is of interface static type (as reflect.TypeOf() returns the dynamic type of the interface as already documented in go doc reflect.TypeOf)

I've submitted the following CL as a documentation suggestion: https://go-review.googlesource.com/c/go/+/586275

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationIssues describing a change to documentation.FixPendingIssues that have a fix which has not yet been reviewed or submitted.FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.compiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions