gob 在解码时向我的对象附加乱码
我试图对 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢 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/