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: non-nil empty slices encoded/decoded as nil #10905

Closed
robzan8 opened this issue May 19, 2015 · 7 comments
Closed

encoding/gob: non-nil empty slices encoded/decoded as nil #10905

robzan8 opened this issue May 19, 2015 · 7 comments

Comments

@robzan8
Copy link

@robzan8 robzan8 commented May 19, 2015

Take an empty slice (non nil), encode it with gob, decode it again and you get a nil slice:
https://play.golang.org/p/WI8NCohVua
(I only tested on the playground for now)

@robzan8 robzan8 changed the title gob-encoded empty slices decoded as nil gob: non-nil empty slices encoded/decoded as nil May 19, 2015
@cespare
Copy link
Contributor

@cespare cespare commented May 19, 2015

I don't see why this is a bug. From the encoding/gob docs:

When a slice is decoded, if the existing slice has capacity the slice will be extended in place; if not, a new array is allocated.

Your existing nil slice does, in fact, have capacity for 0 elements, so no new slice needs to be created.

@minux minux changed the title gob: non-nil empty slices encoded/decoded as nil encoding/gob: non-nil empty slices encoded/decoded as nil May 19, 2015
@robzan8
Copy link
Author

@robzan8 robzan8 commented May 19, 2015

Oh maybe I get it. When decoding, it doesn't necessarily allocate a new slice. It just tries to append the encoded elements to the destination slice. And since there are no encoded elements, nothing happens. Is that correct?

@minux minux closed this May 19, 2015
@minux
Copy link
Member

@minux minux commented May 19, 2015

@robzan8
Copy link
Author

@robzan8 robzan8 commented May 19, 2015

Thanks, sorry for wasting your time

@allaud
Copy link

@allaud allaud commented Apr 9, 2016

What is the workaround for the issue, btw? I faced exactly the same problem recently: http://play.golang.org/p/lSFZCkPaRd

@robzan8
Copy link
Author

@robzan8 robzan8 commented Apr 9, 2016

There is no general workaround, your code just has to deal with this in a specific way that depends on what you are trying to accomplish.
If your goal is to make a deep copy, there are approaches that avoid gob entirely.
golang-nuts is probably a better place to discuss this.
https://groups.google.com/forum/#!forum/golang-nuts

@safareli
Copy link

@safareli safareli commented Nov 10, 2016

json encode is working differently in that case (and I think properly), Becuase of that users might face this kinde of inconsistancies: (lint to playground)

package main

import (
    "bytes"
    "encoding/gob"
    "encoding/json"
    "fmt"
)

type User struct {
    FriendIDs []int `json:friends`
}

func main() {
    var network bytes.Buffer
    enc := gob.NewEncoder(&network)
    dec := gob.NewDecoder(&network)
  inUser1 := User{nil}
  inUser2 := User{[]int{}}
  inJson1, _ := json.Marshal(inUser1)
  inJson2, _ := json.Marshal(inUser2)
    _ = enc.Encode(inUser1)
    _ = enc.Encode(inUser2)

    var outUser1 User
    var outUser2 User
    _ = dec.Decode(&outUser1)
  outJson1, _ := json.Marshal(outUser1)
    _ = dec.Decode(&outUser2)
  outJson2, _ := json.Marshal(outUser2)
  fmt.Printf("outJson1: %s\n", outJson1)
  fmt.Printf(" inJson1: %s\n", inJson1)
  fmt.Printf("outJson2: %s\n", outJson2)// here is the issue gob is treating nil and []int{} same way when json is treating them properly
  fmt.Printf(" inJson2: %s\n", inJson2)
}

I think gob should trate nil and []int{} differently as, if you you json.marshal some stract and return json string to client you get proper response (empty array instead of null). But if you store the struct i same cache using gob encode/decode and and then send response to client using json.marshal you get null instead of empty array.

@golang golang locked and limited conversation to collaborators Nov 10, 2017
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
6 participants
You can’t perform that action at this time.