gob

use gob to package recursively defined structs

瘦欲@ 提交于 2019-12-05 22:20:07
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" "io/ioutil" ) type Order struct { Text string User *User } type User struct { Text string Order *Order } func main() { o := Order{} u := User{} o.Text = "order text" u.Text = "user text" // commenting this section prevents stack overflow o.User = &u u.Order = &o fmt.Println("o.u.text:", o.User.Text, "u.o.text:", u.Order.Text) // end section m := new(bytes.Buffer) enc :=

difference between encoding/gob and encoding/json in golang

怎甘沉沦 提交于 2019-12-05 19:15:34
问题 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 information(https://golang.org/pkg/encoding/): gob Package gob manages streams of gobs - binary values exchanged between an Encoder (transmitter) and a Decoder (receiver). json Package json implements encoding and decoding of JSON as defined in RFC 4627. Can someone explain to me whether

Is encoding/gob deterministic?

余生颓废 提交于 2019-12-04 03:26:07
问题 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 non-deterministic when maps are involved. This is due to the random iteration order of the maps, resulting in their serialisation to be randomly ordered. 回答1: You shouldn't really care as long as it "gets the job done". But current encoding/gob

Store map key/values in a persistent file

徘徊边缘 提交于 2019-12-03 11:56:26
问题 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 into a map and lookup values (Hash and LastModified) using the key (Path). Is there a slick way of doing this in Go? If not, what file format can you recommend? I have read about and experimented with with some key/value file stores in previous projects, but not

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

╄→гoц情女王★ 提交于 2019-12-03 04:55:53
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 CloneObject(a, b interface{}) []byte { buff := new(bytes.Buffer) enc := gob.NewEncoder(buff) dec := gob.NewDecoder(buff) err := enc.Encode(a) if err != nil { log.Panic("e1: ", err) } b1 := buff.Bytes() err = dec.Decode(b) if err != nil { log.Panic("e2: ", err) } return b1 } func main() { var a interface{} a = map[string]interface{}{"X": 1} b2, err := json.Marshal(&a) fmt

Quicker way to deepcopy objects in golang

僤鯓⒐⒋嵵緔 提交于 2019-11-30 20:17:49
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 gob encoding should be quicker. But I see exact opposite behaviour. Can someone tell me if I am doing something wrong? Or any better and quicker way to deepcopy than these two? My object's struct is complex and nested. The test code: package main import ( "bytes" "encoding/gob" "encoding/json" "log" "time" "strconv" ) // Test ... type Test struct

Quicker way to deepcopy objects in golang

谁说我不能喝 提交于 2019-11-30 04:26:14
问题 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 gob encoding should be quicker. But I see exact opposite behaviour. Can someone tell me if I am doing something wrong? Or any better and quicker way to deepcopy than these two? My object's struct is complex and nested. The test code: package main

Efficient Go serialization of struct to disk

女生的网名这么多〃 提交于 2019-11-27 14:11:40
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" "encoding/gob" "fmt" ) type Entry struct { Key string Val string } func main() { var buf bytes.Buffer enc := gob.NewEncoder(&buf) e := Entry { "k1", "v1" } enc.Encode(e) fmt.Println(buf.Bytes()) } This produces a lot of bloat that I don't need: [35 255 129 3 1 1 5 69 110 116 114 121 1 255 130 0 1 2 1 3 75 101 121 1 12 0 1 3 86 97 108 1 12 0 0 0 11 255 130 1 2