Questions tagged [gob]

gob is a Go specific protocol and package for the exchange of data between Go programs. Any question having this tag should also be tagged with [go].

The Go protocol defines a solution for the binary encoding and decoding of go data, aimed at exchange between Go programs, in which it is supported by the gob package.

The Go playground has an example by the Go team :

package main

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

type P struct {
    X, Y, Z int
    Name    string
}

type Q struct {
    X, Y *int32
    Name string
}

func main() {
    // Initialize the encoder and decoder.  Normally enc and dec would be
    // bound to network connections and the encoder and decoder would
    // run in different processes.
    var network bytes.Buffer        // Stand-in for a network connection
    enc := gob.NewEncoder(&network) // Will write to network.
    dec := gob.NewDecoder(&network) // Will read from network.
    // Encode (send) the value.
    err := enc.Encode(P{3, 4, 5, "Pythagoras"})
    if err != nil {
        log.Fatal("encode error:", err)
    }
    // Decode (receive) the value.
    var q Q
    err = dec.Decode(&q)
    if err != nil {
        log.Fatal("decode error:", err)
    }
    fmt.Printf("%q: {%d,%d}\n", q.Name, *q.X, *q.Y)
}
111 questions
3
votes
2 answers

How to send map using gob in golang?

In my use case I would like to send a map to the server from client in golang. I am using gob package to encode and decode the object. In the server end I am not able to decode the object. Server: package main import ( "encoding/gob" …
Dany
  • 2,692
  • 7
  • 44
  • 67
3
votes
3 answers

Gob Decoder Returning EOF Error

I am attempting to implement an interface based message queue where jobs are pushed as bytes to a redis queue. But I keep receiving an EOF error when attempting to decode the byte stream. https://play.golang.org/p/l9TBvcn9qg Could someone point me…
alienchow
  • 383
  • 2
  • 7
3
votes
1 answer

Append to golang gob in a file on disk

I'm trying to save gob-encoded data in a file on disk as a simple datastore. However, when I open it next time the gob encoder just ignores whatever data is already in the file, and starts over sending definitions of already sent formats before…
Filip Haglund
  • 13,919
  • 13
  • 64
  • 113
3
votes
1 answer

Is there a way to serialize cyclic data structures with encoding/gob?

I'm working on porting a neural network library to Go. I want to be able to save and restore a trained network, so I'm attempting to serialize it directly. The problem is, the network struct contains cycles in its field (Neuron A has a connection to…
Joel
  • 1,437
  • 2
  • 18
  • 28
3
votes
1 answer

Using Snappy compression on Gob data in Go?

I need to save a structure to disk and read it in again later, I'm trying to keep IO down to a minimum, but also not spend ages compressing and uncompressing the file, so I intend to use Snappy for compression as it's very fast and relatively…
Alasdair
  • 13,348
  • 18
  • 82
  • 138
2
votes
1 answer

Retrieval after serialization to disk using gob

I have been learning about databases and wanted to implement one as well for learning purposes and not for production. I have a defined schema: type Row struct { ID int32 Username string Email string } Now, currently, i am able…
Kwaku Biney
  • 327
  • 1
  • 3
  • 11
2
votes
1 answer

Go: Implementing a ManyDecode for a "set" of individual results

I have implemented a very simple Decode method (using gob.Decoder for now) - this works well for single responses - it would even work well for slices, but I need to implement a DecodeMany method where it is able to decode a set of individual…
Peter
  • 3,144
  • 11
  • 37
  • 56
2
votes
1 answer

Two gob encoders produce different results

... and it's driving me nuts trying to understand what I'm doing wrong! Playground: https://go.dev/play/p/ZQP8Y-gwihQ The example looks contrived but it's drawn from code that I have where the error arose. In my code I'm hashing the bytes buffer and…
DazWilkin
  • 32,823
  • 5
  • 47
  • 88
2
votes
0 answers

How to decode different types with Golang gob package

I need to write a client and a server. Client can send different request types as a GoLang struct, and server should recognize the type, and invoke a corresponding handler function. How would you achieve that? I tried to look into gob package, but I…
Mark
  • 423
  • 4
  • 12
2
votes
0 answers

I registered the type but still get error "gob: type not registered for interface:"

I can't seem to figure out why a custom struct is not Registered with encoding/gob This error happens when I attempt to save the Gorilla session: could not save session: securecookie: error - caused by: securecookie: error - caused by: gob: type…
BryanWheelock
  • 12,146
  • 18
  • 64
  • 109
2
votes
2 answers

Why decode two different objects into a same object, but the bool member has not been changed?

I'm using go's encoding/gob to decode two different objects which type are T into a same object, but the object's bool member has not been changed after the second decode. Why? package main import ( "fmt" "encoding/gob" "bytes" ) type…
hhdidid
  • 35
  • 5
2
votes
1 answer

Serializing a struct to send it via UDP

I'm trying to figure out how to serialize a struct and send it via UDP to my server. I managed to actually send the struct but upon receiving I do not have any values in it... Except when I statically add a number. In the following code I am…
Fabio Oesch
  • 143
  • 2
  • 11
2
votes
1 answer

Gob can't encode map with a nil pointer value

Gob's Encode returns an error when I try to encode a map to pointers if one of the values is nil. This seems to contradict the docs (but I may be misinterpreting the meaning): In slices and arrays, as well as maps, all elements, even zero-valued…
rampatowl
  • 1,722
  • 1
  • 17
  • 38
2
votes
1 answer

DeepEqual incorrect after serializing map into gob

I've encountered some strange behavior with reflect.DeepEqual. I have an object of type map[string][]string, with one key whose value is an empty slice. When I use gob to encode this object, and then decode it into another map, these two maps are…
rampatowl
  • 1,722
  • 1
  • 17
  • 38
2
votes
1 answer

Go error: gob: type sync.Mutex has no exported fields

I am having an issue where I can't save a struct as a gob if it has an exported sync.Mutex. Everything seems to work if I make the mutex unexported (by not capitalizing it). I'm curious to understand why this is and make sure that there's no other…
rampatowl
  • 1,722
  • 1
  • 17
  • 38