Skip to content
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: Setting embedded error fails as "unexported field" #44641

Closed
work-robot opened this issue Feb 26, 2021 · 1 comment
Closed

reflect: Setting embedded error fails as "unexported field" #44641

work-robot opened this issue Feb 26, 2021 · 1 comment

Comments

@work-robot
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.16 linux/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/robot/.cache/go-build"
GOENV="/home/robot/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/robot/gopath/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/robot/gopath"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/robot/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/robot/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.16"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build856442502=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I've embedded error into a struct, and tried to set this via reflection:

https://play.golang.org/p/8dEZLTljtI3

What did you expect to see?

I can set the embedded field.

What did you see instead?

panic: reflect: reflect.Value.Set using value obtained using unexported field

It looks as if error is considered an unexported field based on the type name.

@ianlancetaylor
Copy link
Member

This is consistent with the language itself. If you split your code into two separate packages, the importing package will also not be able to set the error field. The compiler will give an error like cannot refer to unexported field or method error. The reflect package always acts as though it is in a separate package (because it is) so this is working as intended.

@golang golang locked and limited conversation to collaborators Feb 26, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants