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: documentation incomplete(or wrongly implemented) #29057

Closed
JAicewizard opened this issue Dec 1, 2018 · 6 comments
Closed

encoding/gob: documentation incomplete(or wrongly implemented) #29057

JAicewizard opened this issue Dec 1, 2018 · 6 comments

Comments

@JAicewizard
Copy link

@JAicewizard JAicewizard commented Dec 1, 2018

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

go version go1.11.2 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
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/jaap/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/jaap/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/lib/go"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/jaap/go-projects/templatebuilder/go.mod"
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-build816382028=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I implemented the gob.GobEncoder (and gob.GobDecoder) interface(s) using pointer receivers on both ends.

What did you expect to see?

I expected to see gob calling my encoder and decoder to transmit the data

What did you see instead?

Gob doesn't use my encoder/decoder.
After debugging I could see that it does recognize that it implements both GobEncoder and GobDecoder, but for some reason doesn't use them.
After removing the * from the Encoder(making it a non-pointer receiver) receiver gob does use my implementation(successfully).

UPDATE:
I know that this is because I pass a non-pointer to enc.Encode() but this is was a bit frustrating.
Maybe just add a note that using a pointer receiver only works with pointer values, while non-pointer receivers work with both pointer and non-pointer receivers.

@agnivade
Copy link
Contributor

@agnivade agnivade commented Dec 2, 2018

/cc @robpike for further comments.

@robpike
Copy link
Contributor

@robpike robpike commented Dec 2, 2018

Please show a complete, compact program that illustrates the problem.

@JAicewizard
Copy link
Author

@JAicewizard JAicewizard commented Dec 2, 2018

https://play.golang.org/p/idvKWcdTPiI

By commenting out line 36(BWithPointerData()) you can see that it works when a pointer is given and not vice versa.
I was a bit confused at the time of reporting this, I think a simple warning/comment in the documentation warning about this would have made my day a lot easier, and I expect this will help other people too.

@robpike
Copy link
Contributor

@robpike robpike commented Dec 2, 2018

This is how Go works. I'm not sure it's the gob's package job to explain the difference between the method sets of pointers and values.

https://golang.org/doc/faq#different_method_sets

@JAicewizard
Copy link
Author

@JAicewizard JAicewizard commented Dec 3, 2018

I think it would be more convenient if there would be a reference to the faq in the gob(and JSON...) documentation.
If you encounter something you can't figure out, you look at the definition(of the interface in this case), not the general faq.
Although I thought it "worked" with JSON, it also doesn't encode with a pointer method, but it does decode using the unmarshal method(unlike gob).

@agnivade
Copy link
Contributor

@agnivade agnivade commented Feb 26, 2019

If you encounter something you can't figure out, you look at the definition(of the interface in this case), not the general faq.

If that "something" is not related to that type, I think that expectation is incorrect. If I am working with encoding/json, and I cannot figure out how slice append work, I do not expect that to be explained in the interface definition.

Method sets are a design of the programming language itself and not specific to any package. I don't think adding a link to the FAQ just in gob and json, for something that is general to the programming language, is very helpful.

It is clearly documented in the spec and also in the FAQ.

I am going to close this as the original issue title is resolved. There is no issue with encoding/gob. If you feel method sets need to be documented more prominently, please feel free to open an issue with suggestions, and with reasoning explaining why the current documentation is not enough. Thanks.

@agnivade agnivade closed this Feb 26, 2019
@golang golang locked and limited conversation to collaborators Feb 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.