-
Notifications
You must be signed in to change notification settings - Fork 18k
encoding/gob: deserializes internal Struct{&0} as nil #12039
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
Comments
A consequence of the design: If every field is zero, there is simply no way to transmit the existence of the item on the other end. And who is to say that is not the correct behavior anyway? In many cases it will be. Working as intended when it's not just unfortunate. |
Why is there no way to transmit existence? For a raw *int, gob can On Wed, Aug 5, 2015, 21:16 Rob Pike notifications@github.com wrote:
|
It is inherent in the design. Top-level items are always sent even if they are zero, whatever their type (http://play.golang.org/p/VJxmBFGnFH). Internal items are not, and the difference between T and *T in an internal item has no bearing on what is transmitted. This is the design. Working as intended. |
It is very unfortunate then that Go has no lossless data serializer (e.g. Can you at least change gob's documentation to make this more clear? Also, information losses of the encoding shouldn't be hidden in the wire Would that be viable? On Wed, Aug 5, 2015, 22:58 Rob Pike notifications@github.com wrote:
|
The question of what is guaranteed is a little fuzzy. There might be a way to do this, but I worry about compatibility. It may be possible to send a zero struct but not its contents but is sure to break some user. Re-opening for trial early in 1.6. |
Working as intended, if not the way you want.. A zero struct should behave the same as a zero int. There is no notion of "existence" only "non-zero". |
http://play.golang.org/p/h-F_TgKdcj A zero struct certainly behaves different from a zero int. If this is WAI, then I'd highly appreciate if the documentation of gob could be changed to actually explain these things. |
Until now I thought gob serializing + deserializing will recreate the object so that it is equal to the original according to reflect.DeepEqual. In fact, isn't this the one most basic requirement for serialization?
However, this is a case where it won't:
http://play.golang.org/p/CWP5m7RKIg
Expected behavior: for any object a, gob.NewEncoder(...).Encode(a) will
Actual behavior: it silently turns the allocated zero into a nil.
Note that gob's documentation is ambiguous about this; it first writes:
"Pointers are not transmitted, but the things they point to are transmitted; that is, the values are flattened."
which is nice, but later - in the encoding details - writes:
"If a field [of a struct] has the zero value for its type, it is omitted from the transmission."
When reading it first, I assumed this "zero value" only includes nil because the field is pointer-typed. However, it seems like the zero value of the underlying type is considered a zero value here too.
Therefore I suppose there are two ways to fix it:
The text was updated successfully, but these errors were encountered: