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
37
votes
2 answers

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

gob fails to encode map[string]interface{} gob: type not registered for interface: map[string]interface {} http://play.golang.org/p/Si4hd8I0JE package main import ( "bytes" "encoding/gob" "encoding/json" "fmt" "log" ) func…
Artem
  • 1,307
  • 2
  • 11
  • 23
25
votes
3 answers

Write to Client UDP Socket in Go

I'm looking for a good solution for a client/server communication with UDP sockets in Go language. The examples I found on the Internet show me how to send data to the server, but they do not teach how to send them back to the client. To…
Aleksandrus
  • 1,589
  • 2
  • 19
  • 31
20
votes
1 answer

Unable to send gob data over TCP in Go Programming

I have a client server application, using TCP connection Client: type Q struct { sum int64 } type P struct { M, N int64 } func main() { ... //read M and N ... tcpAddr, err := net.ResolveTCPAddr("tcp4", service) ... …
Emanuel
  • 6,622
  • 20
  • 58
  • 78
19
votes
1 answer

Quicker way to deepcopy objects in golang, JSON vs gob

I am using go 1.9. And I want to deepcopy value of object into another object. I try to do it with encoding/gob and encoding/json. But it takes more time for gob encoding than json encoding. I see some other questions like this and they suggest that…
Rohanil
  • 1,717
  • 5
  • 22
  • 47
17
votes
2 answers

difference between encoding/gob and encoding/json

I am writing an application in Go which uses encoding/gob to send structures and slices over UDP between nodes. It works fine but I notice that encoding/json also has the similar API. Searched and found this…
Anu
  • 400
  • 1
  • 4
  • 19
17
votes
2 answers

De- and encode interface{} with Gob

I'm trying to de- and encode a struct which contains a Interface{} as field. The problem there is, that the encoding works fine, but if I try to decode the data to data the value gets { }. It actually works, if I change Data interface{} to…
Testuser
  • 1,717
  • 2
  • 16
  • 24
15
votes
1 answer

How to convert []byte to *bytes.Buffer

I am trying to decode a gob output that I am sending through another fasthttp endpoint and receiving an error Fasthttp endpoint(encode []string through gob) ----> Fasthttp endpoint( receive and decode) buffer := &bytes.Buffer{} buffer =…
rithik r
  • 191
  • 1
  • 1
  • 5
12
votes
3 answers

Store map key/values in a persistent file

I will be creating a structure more or less of the form: type FileState struct { LastModified int64 Hash string Path string } I want to write these values to a file and read them in on subsequent calls. My initial plan is to read them…
Nate
  • 5,237
  • 7
  • 42
  • 52
12
votes
3 answers

What's the purpose of gob.Register method?

I have read the documentation of ( gob) and I have some problems : Now I know how to encode structure and decode like that: func main() { s1 := &S{ Field1: "Hello Gob", Field2: 999, } log.Println("Original value:", s1) …
user5280774
10
votes
3 answers

Efficient Go serialization of struct to disk

I've been tasked to replace C++ code to Go and I'm quite new to the Go APIs. I am using gob for encoding hundreds of key/value entries to disk pages but the gob encoding has too much bloat that's not needed. package main import ( "bytes" …
bitloner
  • 1,581
  • 5
  • 12
  • 13
9
votes
1 answer

Gob decode cannot decode interface after register type

I have these types defined: func init() { gob.RegisterName("MyMessageHeader", MyMessageHeader{}) gob.RegisterName("OtherMsg", OtherMsg{}) } // // Messages // type MyMessageHeader struct { MessageId InstanceIdType OtherId …
Netwave
  • 40,134
  • 6
  • 50
  • 93
9
votes
3 answers

Is encoding/gob deterministic?

Can we expect for two Go objects x, y such that x is equal to y (assuming no trickiness with interfaces and maps, just structs and arrays) that the output of gob_encode(x) and gob_encode(y) will always be the same? edit (Jun 8 2018): gob encoding is…
dpington
  • 1,844
  • 3
  • 17
  • 29
7
votes
1 answer

Function which accepts array of arbitrary size as argument (is it possible in Golang?)

Q: Is there a way, in golang, to define a function which accepts an array of arbitrary length as argument? e.g., function demoArrayMagic(arr [magic]int){ .... } I have understood that in golang, array length is part of the variable type, for this…
Fabiano Tarlao
  • 3,024
  • 33
  • 40
6
votes
2 answers

How to tell Golang Gob encoding that it’s ok to serialize a struct that contains a struct with no exported fields

I believe this is a legitimate use case for Gob serialization. Yet enc.Encode returns an error because Something has no exported field. Note that I’m not serializing Something directly but only Composed that contains exported fields. The only…
Franck Jeannin
  • 6,107
  • 3
  • 21
  • 33
6
votes
2 answers

gob panics decoding an interface

I have a struct with unexported fields which should be gob encoded and decoded. Say: type A struct { s int } func (a *A) Inc() { a.s++ } Obviously in that case I need to implement gob.GobEncoder and gob.GobDecoder interfaces. And if I use…
yalegko
  • 63
  • 5
1
2 3 4 5 6 7 8