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.StructOf: Exported methods are generated for embedded fields #20632

Closed
abhinav opened this issue Jun 9, 2017 · 1 comment
Closed

Comments

@abhinav
Copy link
Contributor

abhinav commented Jun 9, 2017

Please answer these questions before submitting your issue. Thanks!

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

$ go version
go version go1.8.3 darwin/amd64

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

$ go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/abg/dev/go"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.8.3/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.8.3/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/8z/qdzjsr3n5l72vdg67zr6xkjc0000gn/T/go-build554429180=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

What did you do?

The documentation for reflect.StructOf states,

StructOf currently does not generate wrapper methods for embedded fields.
This limitation may be lifted in a future version.

So the expectation is that if I generate a struct using StructOf that embeds
*bytes.Buffer, it does not implement io.Writer.

https://play.golang.org/p/R9mDLLNMje demonstrates that this is not true: the
generated struct claims to implement io.Writer, can be casted to it, and a
Write call on that io.Writer gets delegated to the right *bytes.Buffer.

What did you expect to see?

Implements io.Writer: false
Can cast to io.Writer: false

What did you see instead?

Implements io.Writer: true
Can cast to io.Writer: true
Wrote: hello

See also #20633 for inconsistent behavior when the method is unexported.

@ianlancetaylor
Copy link
Contributor

The current situation is that it sometimes works and sometimes doesn't. Try adding another field to your StructOf call, such that the interface is not the first field. Horrible things will happen.

The general issue is #15924. Let's move any discussion there. Closing this issue.

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