FAQ
It's possible to decode arbitrary JSON data[1<http://golang.org/doc/articles/json_and_go.html>],
without having a struct that represents the data.

*Decoding arbitrary data*

Consider this JSON data, stored in the variable b:

b := []byte(`{"Name":"Wednesday","Age":6,"Parents":["Gomez","Morticia"]}`)

Without knowing this data's structure, we can decode it into an interface{} value
with Unmarshal:

var f interface{}
err := json.Unmarshal(b, &f)

I want to do something similar with "encoding/gob", namely decode arbitrary
data stored in there, in a situation where I don't have the concrete struct
that was used while encoding.

According to goals of gobs[2<http://golang.org/doc/articles/gobs_of_data.html>],
it sounds like it should be possible,

Gob streams must be self-describing. Each gob stream, read from the
beginning, contains sufficient information that the entire stream can be
parsed by an agent that knows nothing a priori about its contents. This
property means that you will always be able to decode a gob stream stored
in a file, even long after you've forgotten what data it represents.

However, there's a problem because Go is not a dynamic language and there
currently is no way to create types in run-time.

According to this thread[3<https://groups.google.com/forum/?fromgroups=#!topic/golang-nuts/d-vW6Ab31Gg>
],
But it seems that to read a stream of gobs, you need to know the
type of each gob in advance. I cannot decode any gob into
interface{}. Why not?
If you send a struct on a gob, the receiver needs to receive that into
a struct type. If you don't tell it the struct type to receive into,
the receiving gob package would have to create the struct type on the
fly. But the reflect package, and the Go runtime, has no facility for
that.


I accept that for now.

But it should still be possible to get a textual representation of the
struct type that was used in encoding.

Say I do something like...

type P struct {
X, Y, Z int
Name string
}

err := enc.Encode(P{3, 4, 5, "Pythagoras"})

To produce a slice of bytes.
*
*
*Input:* a slice of bytes of an encoded gob and nothing else (i.e. type P
is absent)
*Output: *the following string or something that contains the following
type information "type P = struct { X int; Y int; Z int; Name string; }"

How should I go about doing that? Thank you.

[1] http://golang.org/doc/articles/json_and_go.html
[2] http://golang.org/doc/articles/gobs_of_data.html
[3]
https://groups.google.com/forum/?fromgroups=#!topic/golang-nuts/d-vW6Ab31Gg

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Rob Pike at Mar 10, 2013 at 12:10 am
    Look at the code in encoding/gob/debug.go. It shows you how to parse
    the stream without compiled-in types. It wouldn't be too hard to some
    mappy thing based on that, not that I recommend doing so.

    -rob

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Dmitri Shuralyov at Mar 10, 2013 at 8:53 pm
    Thank you, I'll give that a shot!

    What do you mean by "not that I recommend doing so"?

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMar 9, '13 at 11:28p
activeMar 10, '13 at 8:53p
posts3
users2
websitegolang.org

2 users in discussion

Dmitri Shuralyov: 2 posts Rob Pike: 1 post

People

Translate

site design / logo © 2021 Grokbase