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: encode zero will get the previous value. #21929

Closed
ByzanTine opened this issue Sep 19, 2017 · 2 comments
Closed

encoding/gob: encode zero will get the previous value. #21929

ByzanTine opened this issue Sep 19, 2017 · 2 comments

Comments

@ByzanTine
Copy link

@ByzanTine ByzanTine commented Sep 19, 2017

Please answer these questions before submitting your issue. Thanks!

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

Just playing with playground.

Does this issue reproduce with the latest release?

N/A

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

N/A

What did you do?

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

What did you expect to see?

1
0
-1

What did you see instead?

1
1
-1

As I read through some similar issues, it seems that when decoding, the object buffer will not be cleared out. So I guess this may be a expected outcome as in language spec. Then I think in example code, it should be strongly discouraged to reuse the object buffer.

But, as I was reading examples (https://golang.org/src/encoding/gob/example_test.go). The example clearly reuse the object q to read decoded value twice. As a C/C++ programmer, there is strong inclination to reuse a buffer if possible.

This behavior is much more deadly as I was transmitting a boolean, a false never reaches if the previous value is true. This means this library is very easy to use wrongly.

@cznic
Copy link
Contributor

@cznic cznic commented Sep 19, 2017

IINM, this is working as intended and cannot be changed. It's how zero-values handling was designed to work and it saves wire-data when there's a lot of them. I think the designer(s) had experienced zero-values being common enough to justify this.

@robpike
Copy link
Contributor

@robpike robpike commented Sep 19, 2017

Working as intended. Start with a clean, zeroed item before decoding.

@robpike robpike closed this Sep 19, 2017
@golang golang locked and limited conversation to collaborators Sep 19, 2018
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.