我正在制作一个 go 程序,我需要将 gob 写入文件。我使用 .String() 方法将 gob 转换为字符串。
var network bytes.Buffer
encoder := gob.NewEncoder(&network)
_ = encoder.Encode(valueToEncode)
gobString := network.String()
然后我将 gob 写入一个文件,稍后我将检索它并将其发送到这个程序:
var filebytes = []byte(file) //i think that these two lines are the issue
network := bytes.NewBuffer(filebytes)
decoder := gob.NewDecoder(network)
var decoded interface{}
_ := decoder.Decode(&decoded)
但是当我运行它时,它给了我这个错误:
gob:编码的无符号整数超出范围
我认为问题在于解码器程序的前两行。那么我应该放什么来正确解码gob?
编辑:我想要的是 gobString 的 .UnString() 方法。我怎样才能做到这一点?