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
0
votes
1 answer

gob.Register name not registered for interface in another package

I have recently restructured my code so that now under the main package there are two packages: chain and api. In chain I defined a few structs SomeStruct1, SomeStruct2 and an interface SomeInterface for those structs. The following is what…
3tbraden
  • 667
  • 1
  • 10
  • 21
0
votes
0 answers

How to calculate hash for every record being written via gob.Encode

My target is to create something like WAL-log and to ensure that data consistent I use some hashing. I have the following code computing hash for every encoded record: package dumper_test import ( "bufio" "bytes" "encoding/gob" …
Chaak
  • 11
  • 3
0
votes
1 answer

gob: type not registered for interface: []interface {}

I am trying to copy a map to another map so i used gob in order to do this. But when i unmarshal a json to map[string]interface{} and then try to copy it to another map i get an error in the encoding part. This is the code: package main import…
0
votes
0 answers

Error while using golang RPC: bad data: undefined type arrayType

I am trying to use the golang RPC calls to pass the structures and I get a error while sending the structures. Currently, whenever I am passing the CNI struct results with Ipv6 parameters then I get the following error: gob: bad data: undefined…
Invictus
  • 2,653
  • 8
  • 31
  • 50
0
votes
2 answers

Deepcopying struct having pointer-to 0 value in golang

I have a struct in golang as below type Test struct { prop *int } I want to take deepcopy of the struct object when prop is pointer-to zero value. The real struct has lot more fields in it and I want deepcopy of entire struct obj. I tried to…
Rohanil
  • 1,717
  • 5
  • 22
  • 47
0
votes
1 answer

TCP `net.Conn.Read` hangs following use of an `encoding/gob` decoder

I can wrap ends of a TCP net.Conn with an encoding/gob en/decoder and en/decode a value through it successfully, but if I follow the Decode with a Read on the raw connection it hangs on the Read: package main import ( "encoding/gob" "net" …
Sean Kelleher
  • 1,952
  • 1
  • 23
  • 34
0
votes
0 answers

Cannot use context.Context as gob RPC call parameter in golang

I try to write a kv-raft server code in Go. (refer this article). However I found there is problem if I want using context.Context in my RPC call parameter // Init of my RPC server kv := new(KVRaft) rpcs :=…
Evan Lin
  • 1,272
  • 1
  • 12
  • 25
0
votes
2 answers

How to persist or encode a linked data structure in Go?

My goal is to have a linked data structure, this is, a struct with a reference to another struct, and so on, so I can encode it into my file system, and whenever I need it, decode it, so I restore the whole linked structure, with the same…
Fernando Á.
  • 7,295
  • 7
  • 30
  • 32
0
votes
1 answer

Error when sending blob of binary data to dynamodb

I'm running into an issue with attempting to manage a dynamodb instance using godynamo. My code is meant to take a gob encoded byte array and put it into dynamodb. func (c *checkPointManager) CommitGraph(pop *Population) { var blob, err =…
Daniel Imberman
  • 618
  • 1
  • 5
  • 18
0
votes
1 answer

En/Decode struct containing many interfaces with different implementations each with gob

I have a quite complex struct that contains many interfaces with each different implementations. For en/decoding that struct in gob I seem to have to register every implementation that could be possibly used for every interface. So I end up with a…
panmari
  • 3,627
  • 3
  • 28
  • 48
0
votes
1 answer

Example of using Gob in stream processing

Gob is good in space and performance efficiency when processing internally repeating data structure like big slice of struct or streamed data as documented. However I can't find a direct example after some searching. Can anyone give or link to an…
Jason Xu
  • 2,903
  • 5
  • 31
  • 54
0
votes
2 answers

Unmarshalling JSON to interface variable while matching the underlying type in Go

I'm writing a wrapper to map with some additional functionality I need. Some of the most important things is the ability to marshal and unmarshal the data while retaining genericity. I managed to write an marshaller using the encoding/gob encoder,…
GolDDranks
  • 3,272
  • 4
  • 22
  • 30
0
votes
1 answer

Go: Sending gob's over a pipe is hanging - UPDATE: Out-of-process http.ResponseWriter is blocking

I'm writing a webserver that distributes requests to out-of-process programs in Go. I'm sending the ResponseWriter and Request datatypes through Pipes using gob. The problem is the external process is hanging when receiving the gob. UPDATE The gob…
user489481
  • 321
  • 1
  • 4
  • 13
-1
votes
1 answer

Go concurrency: Is it a ideal practice using gob.encode or json.Marshal to avoid lock overhead?

I have a big and deeply nested shared struct. Each goroutine of my program may use a different part of the struct, a slice, a map, etc. To make things worse, all these goroutines do long operations, which means it may not be a good idea to use a big…
Steve Wu
  • 143
  • 11
-1
votes
1 answer

How to encode struct to byte slice and decode byte slice back to original struct using gob encoding?

i am trying marshall go struct to bytes (via gob encoding), and then to unmarshall those bytes back to original object. I am getting unexpected result (object is not getting the correct values). Help me to correct the programm please. Input: package…
dancheg
  • 549
  • 1
  • 4
  • 14