<p>The other day I had to read gob data that I didn't actually know the structure for. I looked around and there wasn't a particularly good tool that I could find for easily recreating a gob (there is a Debug function hidden in the encoding/gob package that you can get to if you rebuild the package, but it doesn't print values correctly and it is kinda ugly with type definitions), so I wrote this and figured I'd share it just in case anyone else runs into that problem. <a href="https://github.com/drosseau/degob">https://github.com/drosseau/degob</a> It should work on valid gobs, but I haven't tested too much with anything very complex as my use case was pretty straightforward.</p>
<hr/>**评论:**<br/><br/>robpike: <pre><p>The last time I used Debug, which was quite a while ago I admit, it worked fine. If it's broken I'd like to see it fixed. Please file an issue with a reproducible example at <a href="https://github.com/golang/go/issues/new">https://github.com/golang/go/issues/new</a>.</p></pre>my_dev_acct: <pre><p>Interesting. It definitely doesn't work correctly with the gobs I was trying to read. It was only confusing numeric values (as far as I could tell). uints were being printed as ints. I'll see if I can find an example. I didn't think to file a bug report because I assume the Go authors were already tepid about exposing it since you have to remove the build guard from the file (EDIT: oh oops I didn't realize I was responding to Rob Pike, I guess I'll file a report).</p>
<p>EDIT2: Here is a much more stripped down one, these bytes: <code>0x3 0x6 0x0 0xa</code> should decode as a uint 10 but they print as 5 (they're being decoded as an int, <code>10 >> 1</code>).</p>
<pre><code>Start of debugging
5
</code></pre></pre>sh41: <pre><p>Please file it, as Rob asked:</p>
<blockquote>
<p>Please file an issue with a reproducible example at <a href="https://github.com/golang/go/issues/new" rel="nofollow">https://github.com/golang/go/issues/new</a>.</p>
</blockquote></pre>my_dev_acct: <pre><p>Yep, I will.</p></pre>sh41: <pre><p>Thanks, this can be useful.</p>
<p>I've always known it's possible. From <code>encoding/gob</code> doc:</p>
<blockquote>
<p>A stream of gobs is self-describing. Each data item in the stream is preceded by a specification of its type, expressed in terms of a small set of predefined types.</p>
</blockquote>
<p>So it's always been just a matter of implementing that. I'm surprised it wasn't done earlier. But thanks for your work doing it now.</p></pre>my_dev_acct: <pre><p>I guess it isn't a particularly common need. The only other reference I even saw to someone wanting to do it was met with mostly silence a couple years ago.</p>
<p>I thought it was a little odd that <a href="https://golang.org/src/encoding/gob/debug.go" rel="nofollow">Debug</a> function that I found in the Go source didn't work correctly, but I guess that is why you have to rebuild the package yourself to use it.</p></pre>hobbified: <pre><p>Thank you, this is genuinely useful.</p></pre>