gob 在解码时向我的对象附加乱码

发布于 2025-01-13 17:18:41 字数 444 浏览 1 评论 0原文

我试图对 HTTP 响应进行编码和解码。为了处理正文,我创建了一个自定义 ReadCloser 及其自己的 UnmarshalBinary 和 MarshalBinary 方法。 gob 输出与 UnmarshalBinary 的输出不一致

我还创建了一个示例存储库来演示相同的内容 - https://github.com/slayerjain/gob-decode-issue

我还在 golang 存储库上创建了一个问题 - https://github.com/golang/去/问题/51645

I was trying to encode and decode HTTP responses. to deal with the body I created a custom ReadCloser with its own UnmarshalBinary and MarshalBinary methods. The gob output was inconsistent with the output of the UnmarshalBinary

I also created a sample repo to demonstrate the same - https://github.com/slayerjain/gob-decode-issue.

I've also created an issue on the golang repo - https://github.com/golang/go/issues/51645

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

是你 2025-01-20 17:18:41

感谢 Reddit 上的一位用户,我找到了解决方案。问题是在 UnmarshalBinary 方法中我需要创建字节数组的副本。否则它会被其他数据填充,因为它是一个指针。

参考: https://www.reddit.com/r/golang/comments /tddjdd/gob_is_appending_gibberish_to_my_object/

Thanks to a user on Reddit I found the solution. The problem is that in the UnmarshalBinary method I need to create a copy of the byte array. Else it'll be populated with other data since it's a pointer.

ref: https://www.reddit.com/r/golang/comments/tddjdd/gob_is_appending_gibberish_to_my_object/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文