> Hello all,
>
> In the included code, is there any way to get rid of MetaMsg?
It's a struct with one field, so you could certainly simplify it to
type MetaMsg interface{}
at which point it's clear you don't need it at all.
for {
msg1 := &Msg1{}
err := enc.Encode(msg1)
//err := enc.Encode(&msg1)
if err != nil {
log.Panicf("Encode of Msg1 failed: %#v", err)
}
msg2 := &Msg2{}
err = enc.Encode(msg2)
I couldn't quite figure out how to write the Decode side though.
If I encode a &Msg1 or a &Msg2, is dec.Decode(&msg) with msg an
interface{} supposed to work?
Regards
Albert
> Hello
>
> On Nov 12, 8:46 pm, "Rob 'Commander' Pike" <r...@google.com> wrote:
>> On Nov 12, 2010, at 8:08 AM, Albert Strasheim wrote:
>>> Hello all,
>>> In the included code, is there any way to get rid of MetaMsg?
>> It's a struct with one field, so you could certainly simplify it to
>> type MetaMsg interface{}
>> at which point it's clear you don't need it at all.
>> for {
>> msg1 := &Msg1{}
>> err := enc.Encode(msg1)
>> //err := enc.Encode(&msg1)
>> if err != nil {
>> log.Panicf("Encode of Msg1 failed: %#v", err)
>> }
>> msg2 := &Msg2{}
>> err = enc.Encode(msg2)
>> //err = enc.Encode(&msg2)
>> if err != nil {
>> log.Panicf("Encode of Msg2 failed: %#v", err)
>> }
>> }
>
> I couldn't quite figure out how to write the Decode side though.
>
> If I encode a &Msg1 or a &Msg2, is dec.Decode(&msg) with msg an
> interface{} supposed to work?
it should, if the concrete types on sender and receiver are compatible.
-rob
Maybe I am doing the Register wrong. If I do:
gob.Register(&Msg1{})
...
msg1 := &Msg1{}
err := enc.Encode(&msg1)
...
var msg interface{}
err := dec.Decode(&msg)
it fails with:
gob: wrong type received for local value interface { }
Regards
Albert
Rob is offline for a while though and it will have to
wait until he get back. It is possible that rather than
being a simple bug this is a fundamental problem with
the encoding, that a top-level interface{} cannot be
decoded. Not sure.
Russ
This code works. Is the registering supposed to work for sending
unboxed and receiving boxed?
package main
import (
"bytes"
"fmt"
"gob"
"log"
)
type T int
func main() {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
dec := gob.NewDecoder(&buf)
gob.Register(new(T))
var t interface{} = T(2)
if err := enc.Encode(&t); err != nil {
log.Exitf("encode: %v", err)
}
var i interface{}
if err := dec.Decode(&i); err != nil {
log.Exitf("decode: %v", err)
}
fmt.Println(i)
}
Oops, I forgot to register the type. Thanks.
> Is the registering supposed to work for sending
> unboxed and receiving boxed?
Example?
Russ
package main
import (
"bytes"
"fmt"
"gob"
"log"
)
type T int
func main() {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
dec := gob.NewDecoder(&buf)
gob.Register(new(T))
t := T(2)
if err := enc.Encode(&t); err != nil {
log.Exitf("encode: %v", err)
}
var i interface{}
if err := dec.Decode(&i); err != nil {
log.Exitf("decode: %v", err)
}
fmt.Println(i)
}
Also, is this supposed to work?
package main
import (
"bytes"
"fmt"
"gob"
"log"
)
type T int
func main() {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
dec := gob.NewDecoder(&buf)
gob.Register(new(T))
var t interface{} = new(T)
if err := enc.Encode(&t); err != nil {
log.Exitf("encode: %v", err)
}
var i interface{}
if err := dec.Decode(&i); err != nil {
log.Exitf("decode: %v", err)
}
fmt.Println(i)
}
Neither currently do. The first one can be done without, but the
second one is a major shortcoming. You'd have to embed any pointer
types in structs in order to send them.