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
6
votes
3 answers

Use Gob to write logs to a file in an append style

Would it be possible to use Gob encoding for appending structs in series to the same file using append? It works for writing, but when reading with the decoder more than once I run into: extra data in buffer So I wonder if that's possible in the…
Mahoni
  • 7,088
  • 17
  • 58
  • 115
6
votes
1 answer

Difference between gob decoding an interface in a struct, vs. raw

I have been struggling to understand the difference between encoding/decoding an interface type when that type is embedded in a struct, vs. when it's not embedded at all. Using the following example: here in the playground Note the code declares an…
rolfl
  • 17,539
  • 7
  • 42
  • 76
5
votes
1 answer

How to marshal array to binary and unmarshal binary to array in golang?

I want to use gob to encode and decode object, I do it like this: type transProp struct { a []int b []float64 } func (p transProp) MarshalBinary() ([]byte, error) { // A simple encoding: plain text. var b bytes.Buffer …
roger
  • 9,063
  • 20
  • 72
  • 119
5
votes
2 answers

name not registered for interface

I am trying to send a concrete implementation over RPC. The RPC methods expects a interface. The relevant code snippets are: In package node: type Commander interface { Action() string } type Approach struct { Position int } func (p…
user2128233
  • 53
  • 1
  • 4
4
votes
1 answer

Golang convert gob string to interface

I am making a go program where I need to write a gob to a file. I used the .String() method to convert the gob to a string. var network bytes.Buffer encoder := gob.NewEncoder(&network) _ = encoder.Encode(valueToEncode) gobString :=…
user13484461
4
votes
2 answers

How to serialize a complex interface with unexported fields?

I need to serialize some complex interface (template.Template). It has many unexported fields, and gob don't want to work with them. Any suggestions? P.S. Actualy, I trying to put a parsed template to memcache on App Engine.
troorl
  • 1,579
  • 1
  • 15
  • 20
4
votes
1 answer

Deserializing unknown Go's gob blob

I have gobs of unknown type. Is there way to print it to view inside? There might be gob.Debug but it is not available for me https://golang.org/src/encoding/gob/debug.go Googling advices to use DecodeValue but it requires initialised reflect.Value…
user3130782
  • 841
  • 1
  • 6
  • 15
4
votes
1 answer

Gob Decode Giving "DecodeValue of unassignable value" Error

I'm new-ish to Go and I'm having some trouble putting a gob on the wire. I wrote a quick test that I thought would pass, but the decode call is returning a "DecodeValue of unassignable value" error. Here's the code: type tester struct { Payload…
Tyson
  • 1,685
  • 15
  • 36
4
votes
1 answer

Not able to store data in file properly using gob

When I try to save the map of type map[mapKey]string into a file using gob encoder, it is not saving string in file. Here mapKey is struct and map value is long json string. type mapKey struct{ Id1 string Id2 string } And whenever I am use…
rohan
  • 161
  • 1
  • 1
  • 10
4
votes
1 answer

use gob to package recursively defined structs

I mostly use Python, but am playing around with Go. I wrote the following to do something that is quite simple in python, and im hoping it can be accomplished in Go as well. package main import ( "bytes" "encoding/gob" "fmt" …
domoarigato
  • 2,802
  • 4
  • 24
  • 41
3
votes
1 answer

gob decoder returns only first element in the array

So I was trying to create a mock DB, and in the current implementation, I am trying to make an insert and select which insert rows and select returns them. I decided to use a bytes.Buffer to help maintain a memory block I could insert a slice of…
Kwaku Biney
  • 327
  • 1
  • 3
  • 11
3
votes
1 answer

Decode gob output without knowing concrete types

I'm using gob to serialize structs to disk. The struct in question contains an interface field, so the concrete type needs to be registered using gob.Register(...). The wrinkle here is that the library doing the gob-ing should be ignorant of the…
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
3
votes
0 answers

Is there any Gob MIME type?

Is there any (un)official Gob MIME Type, such as application/x-gob? The official documentation never mentions any MIME type. I am not sure if I should use the same approach as Protobuf, which stopped using specific types in favour of the generic…
basgys
  • 4,320
  • 28
  • 39
3
votes
1 answer

Go. Writing []byte to file results in zero byte file

I try to serialize a structured data to file. I looked through some examples and made such construction: func (order Order) Serialize(folder string) { b := bytes.Buffer{} e := gob.NewEncoder(&b) err := e.Encode(order) if err != nil {…
3
votes
0 answers

Golang Gob Type of next Variable

When using golang -> encoding/gob, I want to determine the type of the next variable I am going to get. One way of doing it, would be to alternate between an enum and the unknown variables, with the enum telling me what the next variable will…
user2089648
  • 1,356
  • 2
  • 17
  • 31