问题标签 [gob]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
4386 浏览

go - 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 interface IFace. It declares a non-exported struct impl. It sets up a few Gob methods to Register, GobEncode, and GobDecode the impl struct.

Then, it also declares a struct Data which is exported, and has a single field Foo which is of the interface type IFace.

So, there's an interface, a struct which implements it, and a container struct which has a field whose value is that interface type.

My problem is that the container struct Data is happily sent through the Gob gauntlet, and, when it goes through, it happily encodes and decodes the IFace field in the Data struct... great! But, I cannot seem to be able to send just an instance of the IFace value through the gob gauntlet.

What is the magic invocation I am missing?

Searching for the error message gives a number of results, but I believe I have satisfied the Gob contract.... and the "proof" of that is in the successful struct gobbing. Obviously I have missed something, but can't see it.

Note, the output of the program is:

The actual code is:

0 投票
0 回答
394 浏览

encoding - Golang Gob 下一个变量的类型

使用 golang -> encoding/gob 时,我想确定下一个要获取的变量的类型。一种方法是在枚举和未知变量之间交替,枚举告诉我下一个变量是什么。

然而,gob-stream 已经知道下一个变量的类型。(或者至少它的名字,这对我来说已经足够好了。)从这个例子中可以看出:

它看到,它得到的下一个东西必须是一个名称为 vector 的结构,并给我一个错误。有没有办法预先查询下一个类型。(同样,没有我明确发送它,实际上会产生不必要的开销)

0 投票
0 回答
1014 浏览

go - 无法在 golang 中使用 context.Context 作为 gob RPC 调用参数

我尝试用 Go 编写一个 kv-raft 服务器代码。(参考这篇文章)。

context.Context但是,如果我想在我的 RPC 调用参数中使用,我发现有问题

可以编译代码但是当我尝试运行时

它会弹出错误为 gob: type not registered for interface: context.emptyCtx

如果我替换*context.Context*string. 我用谷歌搜索它可以使用gob.Register(),但我编译失败了gob.Register(context.Context{})

知道如何context.Context作为 gob RPC 调用参数传递吗?

[更新]

我尝试使用gob.Register(context.Background()),但在接收 RPC 调用时它会得到空上下文。

[更新] 目前我的回购在这里,但仍在进行中。

0 投票
1 回答
1343 浏览

file - 通过附加多次检索写入文件的 gob

我正在尝试使用 encoding/gob 将数据存储到文件并稍后加载。我希望能够将新数据附加到文件并稍后加载所有保存的数据,例如在重新启动我的应用程序之后。使用 Encode() 存储到文件时没有问题,但在阅读时,我似乎总是只得到第一次存储的项目,而不是简洁存储的项目。

这是一个最小的例子: https: //play.golang.org/p/patGkKDLhM

如您所见,它可以向编码器写入两次,然后再将其读回。但是当关闭文件并以附加模式再次重新打开时,写入似乎有效,但读取仅适用于前两个元素(之前已写入)。无法检索两个新添加的结构,我收到错误:

恐慌:缓冲区中的额外数据

我知道在磁盘上的文件中附加到 golang gob并且我还阅读了https://groups.google.com/forum/#!topic/golang-nuts/bn6vjC5Abd8

最后,我还找到了https://gist.github.com/kjk/8015952,这似乎表明我正在尝试做的事情不起作用。为什么?这个错误是什么意思?

0 投票
1 回答
450 浏览

go - TCP `net.Conn.Read` 在使用`encoding/gob` 解码器后挂起

net.Conn我可以用encoding/goben/decoder包装 TCP 的末端并通过它成功地 en/decode 一个值,但是如果我在原始连接上Decode使用 a Read,它会挂在Read

输出:

这会导致 Go Playground 中出现死锁恐慌并挂在我的本地机器 ( go version go1.6.2 darwin/amd64) 上,尽管代码会间歇性地在本地执行到完成。

net.PipeConn如果我使用 a或者如果我Decode使用 aWrite来代替(即交换Read/Write在 en/decode 之后的顺序),则不会发生这种情况。当我删除 en/decode 代码时,en/decode 之后的代码也可以单独工作。

是什么导致了这个挂起?这感觉像是一个缓存问题,但我不知道为什么Write不会刷新或为什么Read不会提取最新的可用数据,或者为什么这个问题只在gob涉及编码/解码时出现。

0 投票
3 回答
13255 浏览

serialization - 结构到磁盘的高效 Go 序列化

我的任务是将 C++ 代码替换为 Go,而且我对 Go API 还是很陌生。我正在使用 gob 将数百个键/值条目编码到磁盘页面,但是 gob 编码有太多不需要的膨胀。

这会产生很多我不需要的膨胀:

我想序列化每个字符串的 len 后跟原始字节,例如:

我保存了数百万个条目,因此编码中的额外膨胀将文件大小增加了大约 x10。

如何在不手动编码的情况下将其序列化为后者?

0 投票
1 回答
2226 浏览

pointers - gob 尝试解码 nil 值导致 EOF 错误

我需要使用gob对一些数据进行编码,但是,我发现无法正确处理“type nil”(转到 1.6.2)

https://play.golang.org/p/faypK8uobF

输出:

0 投票
1 回答
782 浏览

file - 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.

And whenever I am use nested map instead of the struct like:

It is working fine and saving string properly. I am not sure what I am missing here.

Code to encode, decode and save it in file:

0 投票
1 回答
1645 浏览

go - redigo 和 gob 如何检索 gob 数据切片

我正在使用“RPUSH”命令推入我的 redis 基础对象。

Redigo 做我所期待的,它正在推动所有数据结构 gob 编码。

现在我正在尝试检索它们:

对它们进行解码的最佳方法是什么?我想将它们作为接口的一部分返回{}。但即使我的对象被编码为 gob 数据。都是用redis的方式推送的,那么从gob的角度看能不能算是一个切片呢?

我可以迭代其他列表并一一解码。但我对效率没有信心。我假设 gob 想要一个以它的方式编码的切片结构。所以我的问题是:有没有什么技巧可以有效地将我的 gob 数据片段解码为数据结构的集合?或者我应该以另一种方式存储我的数据结构(我假设用 RPUSH 存储我的数据可以防止非原子操作)

0 投票
0 回答
123 浏览

session - beego会话保存结构

几周后我放弃了,想不出一种在beego应用程序之间的会话中编码/解码结构的方法。

问题:如何在会话中正确存储结构app1/models/User并在其他进程/应用程序中对其进行解码,app2/models/User而不从 app1 导入任何内容?

在 app2 中,我们确实知道 struct 结构:

我为 app1 和 app2 整理了两个示例,不确定它们是否有帮助,因为在 app2 中我无法正确解码结构:(。

https://github.com/TalLandder/app1

https://github.com/TalLandder/app2