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
1
vote
1 answer

How do you pass TCP connection objects to other Go modules?

I have a map of hostnames to connections that I am trying to pass around to different modules for an application that I am writing in Go. var conns_ map[string]net.Conn // tcp connections per node In the main server.go file, I dial the other…
Matt
  • 21
  • 6
1
vote
2 answers

GobEncoder for Passing Anonymous Function via RPC

I'm trying to build a system that will execute a function on multiple machines, passing the function anonymously via RPC to each worker machine (a la MapReduce) to execute on some subset of data. Gob doesn't support encoding functions, though the…
tgarv
  • 216
  • 1
  • 3
  • 10
0
votes
1 answer

gob is appending gibberish to my object while decoding

I was trying to encode and decode HTTP responses. to deal with the body I created a custom ReadCloser with its own UnmarshalBinary and MarshalBinary methods. The gob output was inconsistent with the output of the UnmarshalBinary I also created a…
Shubham Jain
  • 431
  • 3
  • 13
0
votes
1 answer

How to separate register of pointer and values?

I am new to gob and am having confusion on gob register. In my code, I have something like var foo1 somestruct var foo2 &somestruct var foo3 anotherstruct func call(a interface, b interface) { var buf bytes.Buffer argsEncoder :=…
ccjeff
  • 1
  • 1
0
votes
1 answer

Golang Gob decoding does not decode array of []byte

I am trying to decode an Inv struct, but decoding the same encoded value returns a different value. // inv struct type Inv struct { AddrFrom string Type int data [][]byte } inv := Inv{ AddrFrom: nodeAddress, Type: …
0x10f2c
  • 15
  • 4
0
votes
1 answer

Wrapping gob decoder

I'm storing encrypted gobs in a k-v database and was hoping to have a convenience function in a package which takes a key and an interface, it decrypts the stored value, decodes it into the passed interface and the consumer code can hum along…
Jon
  • 322
  • 1
  • 11
0
votes
0 answers

Golang - instability with Gob over TCP

I am having an issue with instability when using gob for TCP communication. The main problem is that if i transfer data "to fast" i either get errors where the server terminates the connection or the package simply doesn't arrive at the server. Now,…
JensF
  • 1
  • 1
0
votes
1 answer

How to define a Send/Receive Function with interface{} using gob.Encoder()

I need to define a Send/Receive functions using gob package. I defined Send() simply as follows: func Send(enc *gob.Encoder, PKG interface{}) error { err := enc.Encode(PKG) return err } and Receive as follows: func Receive(dec *gob.Decoder)…
0
votes
1 answer

Consecutive Encode/Decode using GOB

I am new to programming Golang Sockets. When I try to send one message from client to server, it is working perfectly. However, when I try to send 10 consecutive messages, I get an error. Any clues/keywords to search for. Please find enclosed a…
0
votes
0 answers

Attempting to marshal and unmarshal libp2p AddrInfo

I am using the libp2p Go library to make a p2p networking stack. I need to unicast messages to other peers on my network. I want to store the AddrInfo struct on my message I unicast so I can send a response back. The below is libp2p library…
pocockn
  • 1,965
  • 5
  • 21
  • 36
0
votes
0 answers

gob deserialization can not work well on the nested struct

I am writing an rpc call flow. When I use tcp read and write, I found another nested struct problem. I created a request and response object that contains a traceId and struct request object or return object struct, but when I write back the…
DavidDai
  • 5
  • 2
0
votes
0 answers

Unmarshall json or gob to a Go struct with an interface field

I have a struct where merkleTree.MerkleProof is an interface which is implemented by mTree.Proof: type Checkpoint struct { Leaves []Leaf MerkleProof merkleTree.MerkleProof } type MerkleProof interface { Verify(leafHash, treeRoot…
user1731199
  • 237
  • 1
  • 2
  • 10
0
votes
0 answers

Storing to disk after struct -> []byte -> gob -> gzip -> IO -> gzip -> gob -> []byte -> struct

I am trying to store structs, or more accurately the bytes of a struct to disk. I have large arrays of byte data that I want to compress and store on disk and recover later. The data means nothing to any other application except mine. I can encode…
amlwwalker
  • 3,161
  • 4
  • 26
  • 47
0
votes
1 answer

Getting an Error (gob: unknown type id or corrupted data) when trying to decode a blob using gob.Decoder()

I'm trying to to send an encoded blob to a list of local peers(a client code listening at multiple ports on a local machine), at the peers when i decode the blob for some peers it works but for some it doesn't and displays an error(gob: unknown type…
Asim Sansi
  • 39
  • 8
0
votes
0 answers

How to transfer gobs between programs

I use the below code to create a gob file which go successfully executes var METADATA = []interface{}{ map[string]interface{}{...}, map[string]interface{}{...}, ... }…
Brian Yeh
  • 3,119
  • 3
  • 26
  • 40