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

Save nested struct to gob

I'm trying to save a struct to gob, but the file is missing the values of the nested type. I can save the Matrix type on its own, but the gob data of the Network struct doesn't include the Matrix values. Is there a way to save this nested struct to…
Lucas
  • 357
  • 3
  • 15
2
votes
3 answers

Hashing multiple values in golang

I'm currently working on an application in go that needs to cache different resources. Different types of resources have handlers that will know what data is relevant to determine, if we have to rebuild a resource or if we can fetch it from cache.…
Gellweiler
  • 751
  • 1
  • 12
  • 25
2
votes
1 answer

Golang TCPConn Gob Communication

I'm having issues with the gob protocol (or maybe networking in general, where my knowledge is weak), and I don't understand why the following code does not work properly. It is just supposed to be a simple example of maintaining an open TCP…
Qubert
  • 185
  • 3
  • 9
2
votes
1 answer

golang gob converts pointer to 0 into nil pointer

I'm trying to use go's net/rpc package to send data structures. The data structure includes a pointer to uint64. The pointer is never nil, but the value may be 0. I'm finding that when the value is 0, the receiver sees a nil pointer. When the value…
2
votes
1 answer

What does gob encoding do?

Does gob encoding/decoding do anything ? In the example below , data looks the same before and after decoding. I am confused, please advise data = "ABC" buf := new(bytes.Buffer) //glob encoding enc := gob.NewEncoder(buf) …
irom
  • 3,316
  • 14
  • 54
  • 86
2
votes
1 answer

Retrieving gobs written to file by appending several times

I am trying to use encoding/gob to store data to a file and load it later. I want to be able to append new data to the file and load all saved data later, e.g. after restarting my application. While storing to the file using Encode() there are no…
alex
  • 2,252
  • 4
  • 23
  • 34
2
votes
1 answer

How to listen to a client continiously using gob in Golang

In my use case I would like to continuously listen to a TCP connection and receive the value. The expected value is an object. So I am using gob decoder to receive the value from the connection. I would like to continuously listen to the connection…
Dany
  • 2,692
  • 7
  • 44
  • 67
2
votes
1 answer

Gob over the wire

I'm considering using gob ("encoding/gob") for serializing data in a network protocol, I have been searching around and can't seem to find any solution to these problems: Message framing - The gob documentation gives the impression that you can…
Cshark
  • 101
  • 6
2
votes
2 answers

Golang goroutines sharing RPC connection

I have a Golang server which serve inbound requests with dedicated goroutine. Those goroutines would access another backend go server using rpc/gob. In case of reusing the connection to make Gob works better (requests sharing connection may reuse…
Jason Xu
  • 2,903
  • 5
  • 31
  • 54
2
votes
2 answers

Golang gob: type not registered for interface:

So I have a type String that is an alias of string defined: type String string I then apply the following method to it: func (s String) String() string { str := "'" + s + "'" return string(str) } I then try to send a struct containing a…
Charlie Andrews
  • 1,457
  • 19
  • 28
1
vote
1 answer

How to read a gob file line by line

I'm storing Message (struct defined below) inside a file using Gob serialization. type Message struct { Message string `json:"message"` From string `json:"from"` } I managed to do this by putting my Message inside a slice that I…
nem0z
  • 1,060
  • 1
  • 4
  • 17
1
vote
4 answers

panic: gob: type elliptic.p256Curve has no exported fields

I am trying to build a blockchain project when I'm catching an issue about gob Serialize. I have a struct Wallet which uses elliptic.P256() Curve struct, and when I'm trying to serialize Wallet, a bug of no exported fields occured. Really hope for…
Vango Lu
  • 21
  • 1
1
vote
4 answers

Strings encode/decode in gob

I followed https://blog.golang.org/gob link. and wrote a sample, where the structure has all string data. Here is my sample: package main import ( "bytes" "encoding/gob" "fmt" "log" ) type P struct { X string a string …
learner
  • 55
  • 5
1
vote
0 answers

Difference in func with receiver and param golang

I try to create marshal func for struct Item. So the question is, why the first example gives stackoverflow for goroutine and second works correctly? Call method with receiver type Item struct{ a int } func some(items []Item){ for _,i:=range…
NIck
  • 163
  • 1
  • 2
  • 12
1
vote
1 answer

Bug in decoding an rpc reply using gob

Problem: structure's field is not replaced with fresh value if it is zero after an rpc call. Here is minimal code sample: package main import ( "fmt" "log" "net" "net/rpc" ) type RpcObject int type Reply struct { A int } func…
StrangeMann
  • 161
  • 3
  • 9