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

encoding/gob: Encoder.Encode fails to encode maps with keys of type url.URL #32251

Open
opennota opened this issue May 25, 2019 · 3 comments

Comments

@opennota
Copy link

commented May 25, 2019

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

$ go version
go version go1.12.5 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
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/xxx/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/xxx/gocode"
GOPROXY=""
GORACE=""
GOROOT="/home/xxx/go"
GOTMPDIR=""
GOTOOLDIR="/home/xxx/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
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-build478246212=/tmp/go-build -gno-record-gcc-switches"

What did you do?

https://play.golang.org/p/8-8Qws7ogyH

What did you expect to see?

Gob encoder successfully encodes a value of type map[url.URL]something.

What did you see instead?

There's an error: gob: unaddressable value of type *url.URL, despite the fact that the map keys are of type url.URL, not *url.URL.

@mvdan

This comment has been minimized.

Copy link
Member

commented May 26, 2019

Perhaps this is just the error being confusing. One cannot take the address of map keys or values directly, so that's probably the error that the encoder is hitting. Perhaps it could copy the key value to then take the copy's address, but I wonder if that could be unexpected behavior. For example, what if the key type is large and copying is a non-trivial amount of work?

Funnily enough, the spec makes no mention of map key/values not being addressable. It seems to be a limitation of the current Go implementation.

@mvdan

This comment has been minimized.

Copy link
Member

commented May 26, 2019

To clarify my previous comment - the String method uses a pointer receiver, so it can only be called on a non-pointer value if it's addressable. See https://golang.org/pkg/net/url/#URL.String

@opennota

This comment has been minimized.

Copy link
Author

commented May 27, 2019

With a custom type: https://play.golang.org/p/jZJr0eYPal5

The problem seems to be the pointer receiver in MarshalBinary. With a non-pointer receiver encoding/gob works fine, even if I add a String method with a pointer receiver: https://play.golang.org/p/dK7SsqahtAd

@julieqiu julieqiu changed the title encoding/gob: Encoder fails to encode maps with keys of type url.URL encoding/gob: Encoder.Encode fails to encode maps with keys of type url.URL May 28, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.