问题标签 [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 投票
2 回答
7390 浏览

json - encoding/gob 和 encoding/json 的区别

我正在用 Go 编写一个应用程序,它使用编码/gob 在节点之间通过 UDP 发送结构和切片。它工作正常,但我注意到 encoding/json 也有类似的 API。搜索并找到此信息(https://golang.org/pkg/encoding/):

gob 包 gob 管理 gob 流 - 在编码器(发送器)和解码器(接收器)之间交换的二进制值。
json 包 json 实现了 RFC 4627 中定义的 JSON 的编码和解码。

有人可以向我解释一个是否比另一个更有效,并且一般比较何时选择什么?另外,如果我需要与非 Go 应用程序交互,我猜 json 会是首选?

0 投票
1 回答
1699 浏览

file - 去。将 []byte 写入文件会产生零字节文件

我尝试将结构化数据序列化到文件中。我查看了一些示例并进行了这样的构造:

Serialize是一种将其对象序列化为由其id属性调用的文件的方法。我查看了调试器 - 字节缓冲区在写入之前包含数据。我的意思是对象已完全初始化。甚至n表示写入字节数量的变量也超过一千 - 文件根本不应该是空的。该文件已创建,但它完全是空的。怎么了?

0 投票
1 回答
1061 浏览

go - gob 解码器尝试解码为非指针

在我的 Go 程序中,我使用 gob 编码 []byte 数据

但是当我试图解码时得到' gob decoder试图解码成非指针'

0 投票
1 回答
3423 浏览

go - gob编码有什么作用?

gob 编码/解码有什么作用吗?在下面的示例中,解码前后的数据看起来相同。我很困惑,请指教

0 投票
1 回答
858 浏览

go - 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 is non-0, the receives sees a non-nil pointer that points to a proper value. This is problematic, because it means that the RPC is breaking an invariant of my data structure: the pointer will never be nil.

I have a go playground that demonstrates this behavior here: https://play.golang.org/p/Un3bTe5F-P

The output from this code is:

So when Ptr points to a 0, it becomes nil on the receiver side. When Ptr points to 1, it is passed through normally.

Is this a bug? Is there a way around this problem? I want to avoid having to unmarshall my detastructure on the receiver side to fix all the unexpected nil pointers...

0 投票
1 回答
687 浏览

go - Golang RPC 编码自定义函数

我正在尝试使用github.com/dullgiulio/pingo和发送我的自定义结构

但是我总是在nilym Fstruct 领域。这是我在客户端发送的

不能通过 RPC 发送自定义数据类型吗?

0 投票
3 回答
1303 浏览

serialization - Use Gob to write logs to a file in an append style

Would it be possible to use Gob encoding for appending structs in series to the same file using append? It works for writing, but when reading with the decoder more than once I run into:

So I wonder if that's possible in the first place or whether I should use something like JSON to append JSON documents on a per line basis instead. Because the alternative would be to serialize a slice, but then again reading it as a whole would defeat the purpose of append.

0 投票
2 回答
822 浏览

go - gob 恐慌解码接口

我有一个带有未导出字段的结构,应该对其进行 gob 编码和解码。

说:

显然在那种情况下我需要实现gob.GobEncodergob.GobDecoder接口。如果我直接使用结构,一切正常

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

但我还需要一个实现相同逻辑且可序列化的接口:

完整代码: https: //play.golang.org/p/Zig2mPrnrq

突然它惊慌失措:

但是,如果我将 gob 接口注释掉,一切都会再次恢复正常。

我错过了什么重要的东西吗?因为所描述的行为对我来说似乎很奇怪

0 投票
1 回答
561 浏览

go - gob:类型未注册接口:http.gzipReader

我是一个接受连接的简单 TCP 服务器,使用 http.Get 获取 URL,然后使用 gob 对响应进行编码。但是 gob 无法对 http.gzipReader 进行编码。在编码时,它会给出以下错误消息:

我的代码如下:

有没有办法对 http.gzipReaderc 进行编码?我看到了 gob.Register() 的示例,但我正在尝试注册 http.gzipReaderc 。但无法这样做。有人可以建议我解决这个问题吗?

0 投票
1 回答
10011 浏览

go - 如何在golang中将数组编组为二进制和将二进制解组为数组?

我想使用gobtoencodedecodeobject,我这样做:

但是,这个报告错误是这样的: