如何在Go中解码Apache Thrift负载?

huangapple go评论71阅读模式
英文:

How do I decode an Apache Thrift payload in Go?

问题

这是我正在尝试在Go中实现的工作中的JavaScript版本。

let next = TBufferedTransport.receiver(data => {
    let proto = new TCompactProtocol(data);
    let ae = new AnalyticEventBatch();
    ae.read(proto);
});

在Go中,我无法使Thrift解码有效载荷 - 我应该做什么?

英文:

This is the working JavaScript version of what I'm trying to do in Go.

let next = TBufferedTransport.receiver(data => {

 let proto = new TCompactProtocol(data)
 let ae = new AnalyticEventBatch()

 ae.read(proto)
});

Using Go, I can't get Thrift to decode the payload - what should I be doing?

答案1

得分: 4

var data []byte //这是你接收到的字节数组

transp := &TMemoryBuffer{Buffer: bytes.NewBuffer(data)}
proto := NewTCompactProtocol(transp)

ae := NewAnalyticEventBatch()
ae.Read(proto)

如果你可以直接从文件/套接字中读取,会更加清晰。那样的话,你只需要使用 thrift 的 StreamTransport

英文:
var data []byte //that's the byte array you received

transp := &TMemoryBuffer{Buffer: bytes.NewBuffer(data)}
proto := NewTCompactProtocol(transp)

ae := NewAnalyticEventBatch()
ae.Read(proto)

It would be cleaner if you could read from file/socket directly. Then you would only need thrift StreamTransport

huangapple
  • 本文由 发表于 2016年3月2日 01:36:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/35729593.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定