在尝试解码golang中的gob时,我收到“缓冲区中的额外数据”错误

时间:2017-08-10 01:44:02

标签: go decode encode gob

我正在编码并在流上发送多个对象。我解码它们如下面的代码所示,保持连接打开。我在第一个对象之后的所有对象的解码中得到“缓冲区中的额外数据”错误。

func handleAggregatorConnection(conn net.Conn) {
        var connectionNumber = connectionCount
        connectionCount += 1
        log.Println("connection event: starting handle aggregator connection")


        dec := gob.NewDecoder(conn)

        var colorArrays map[string][]string
        colorArrayValue := &colorArrays

        var i P
        ai := &i


        for {
                //err := dec.Decode(colorArrayValue)
                err := dec.Decode(ai)
                if err == nil {
                        receivedColorResultFromAggregator = true
                        //log.Printf("received : %+v", colorArrayValue)
                        log.Println("received:", i)
                        aggregatorResultMap[connectionNumber] = *colorArrayValue
                        log.Println("control server: received object from aggregator ", aggregatorR\
esultMap)
                } else if err == io.EOF {
                        log.Println("reached end of stream while" +
                                "listening to aggregator")
                        delete(aggregatorResultMap, connectionNumber)
                        break
                } else {
                        log.Println("error decoding:", err)
                        break
                }
        }
        log.Println("connection event: closing aggregator connection")
        conn.Close()
}

2 个答案:

答案 0 :(得分:1)

我不同意你的回答,我这样做了一个gobs读者:

func GetAll(db string) ([]*Record, error) {
    r := []*Record{}

    f, err := os.OpenFile(db, os.O_RDONLY, 0644)
    if err != nil {
        return nil, err
    }

    var rr error
    gdec := gob.NewDecoder(f)
    for rr != io.EOF {
        rec := Record{}
        rr = gdec.Decode(&rec)
        if rr != nil {
            continue
        }
        r = append(r, &rec)
    }

    return r, nil
}

它起到了预期的作用。

恕我直言,如果出现错误并继续读取数据,则不应“破解”。

答案 1 :(得分:0)

回答我自己的问题:为每个解码操作创建一个新的解码器使它工作。

func handleAggregatorConnection(conn net.Conn) {
        var connectionNumber = connectionCount
        connectionCount += 1
        log.Println("connection event: starting handle aggregator connection")

        for {

                dec := gob.NewDecoder(conn)

                var colorArrays map[string][]string
                colorArrayValue := &colorArrays

                var i P
                ai := &i



                //err := dec.Decode(colorArrayValue)
                err := dec.Decode(ai)
                if err == nil {
                        receivedColorResultFromAggregator = true
                        //log.Printf("received : %+v", colorArrayValue)
                        log.Println("received:", i)
                        aggregatorResultMap[connectionNumber] = *colorArrayValue
                        log.Println("control server: received object from aggregator ", aggregatorR\
esultMap)
                } else if err == io.EOF {
                        log.Println("reached end of stream while" +
                                "listening to aggregator")
                        delete(aggregatorResultMap, connectionNumber)
                        break
                } else {
                        log.Println("error decoding:", err)
                        break
                }
        }
        log.Println("connection event: closing aggregator connection")
        conn.Close()
}