FAQ
Hi all:
I use msgpack packed an array of interface:



type Header struct {
Version int32
Rpcid int32
CompressFlag int8
CryptFlag int8
CryptReservedData1 string
CryptReservedData2 string
TimeStamp int64
RouteKey []string
}

func New() (r *Header) {
c := []string{"routeip"}
r = new(Header)
r.Version = 1
r.TimeStamp = time.Now().Unix()
r.RouteKey = c
return
}
b := &bytes.Buffer{}
m := []interface{}{
h.Version, // int32
h.Rpcid, // int32
h.CompressFlag, // int8
h.CryptFlag, // int8
h.CryptReservedData1, // string
h.CryptReservedData2, // string
h.TimeStamp, // int64
h.RouteKey} //[]string

_, err := msgpack.PackArray(b, reflect.ValueOf(m))


When I was trying to unpack and assign the value back:

v, _, e := msgpack.Unpack(b)
h := new(Header)

h.Version = v.Index(0).Interface().(int32)

it panic:

panic: interface conversion: interface is int8, not int32

, could anyone give some advice?



--

Search Discussions

  • Damian Gryski at Dec 30, 2012 at 12:52 pm
    There are at least two msgpack implementations in Go. It looks like you're
    using https://github.com/msgpack/msgpack-go . The version
    at https://github.com/ugorji/go-msgpack deals differently with msgpack's
    handling of different integer sizes. Otherwise, you'll need to lean more
    on reflection.

    Damian

    Le samedi 22 décembre 2012 09:58:33 UTC+1, asnip...@gmail.com a écrit :
    Hi all:
    I use msgpack packed an array of interface:



    type Header struct {
    Version int32
    Rpcid int32
    CompressFlag int8
    CryptFlag int8
    CryptReservedData1 string
    CryptReservedData2 string
    TimeStamp int64
    RouteKey []string
    }

    func New() (r *Header) {
    c := []string{"routeip"}
    r = new(Header)
    r.Version = 1
    r.TimeStamp = time.Now().Unix()
    r.RouteKey = c
    return
    }
    b := &bytes.Buffer{}
    m := []interface{}{
    h.Version, // int32
    h.Rpcid, // int32
    h.CompressFlag, // int8
    h.CryptFlag, // int8
    h.CryptReservedData1, // string
    h.CryptReservedData2, // string
    h.TimeStamp, // int64
    h.RouteKey} //[]string

    _, err := msgpack.PackArray(b, reflect.ValueOf(m))


    When I was trying to unpack and assign the value back:

    v, _, e := msgpack.Unpack(b)
    h := new(Header)

    h.Version = v.Index(0).Interface().(int32)

    it panic:

    panic: interface conversion: interface is int8, not int32

    , could anyone give some advice?


    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 22, '12 at 8:59a
activeDec 30, '12 at 12:52p
posts2
users2
websitegolang.org

2 users in discussion

Asniperwolf: 1 post Damian Gryski: 1 post

People

Translate

site design / logo © 2021 Grokbase