ugorji / go

idiomatic codec and rpc lib for msgpack, cbor, json, etc. msgpack.org[Go]
MIT License
1.86k stars 295 forks source link

Calling Encoder.Encode multiple times fails #278

Closed ruz closed 5 years ago

ruz commented 5 years ago
package main

import (
    "bytes"
    "fmt"
    "github.com/ugorji/go/codec"
)

func main() {
    var w bytes.Buffer
    encoder := codec.NewEncoder(&w, new(codec.CborHandle))
    err := encoder.Encode(uint32(123456))
    err = encoder.Encode(uint32(123456))
    fmt.Printf("error: %+v\n", err)
}

This is used to work with older commits.

ugorji commented 5 years ago

Thanks. Will look at it this week.

ugy commented 5 years ago

Same problem here with msgpack handler! Calling Encode only work the first time and return an "index out of range" error afterward. It used to work fine before. Fixed it in my code by calling encoder.Reset() each time I call encoder.Encode but not sure it's normal.

package main

import (
    "bytes"
    "fmt"

    "github.com/ugorji/go/codec"
)

func main() {
    var w bytes.Buffer
    encoder := codec.NewEncoder(&w, new(codec.MsgpackHandle))
    err := encoder.Encode(uint32(123456))
    err = encoder.Encode(uint32(123456))
    fmt.Printf("error: %+v\n", err)
}