douchixu3686 2017-11-22 01:58
浏览 292
已采纳

您如何在Golang中使用Thrift的TMemoryBuffer?

In Go, I have a byte array data []byte which I am trying to read into an object generated by Thrift. In C# the working code is as follows:

var request = new Request();

using (var transport = new TMemoryBuffer(data))
using (var protocol = new TBinaryProtocol(transport))
{
   request.Read(protocol);
}

However in Go, it does not work:

request := app.NewRequest()

transport := thrift.TMemoryBuffer{
    Buffer: bytes.NewBuffer(data),
}
protocol := thrift.NewTBinaryProtocolTransport(transport) // error here
request.Read(protocol)

The error it gives is:

cannot use memoryBuffer (type thrift.TMemoryBuffer) as type thrift.TTransport in argument to thrift.NewTBinaryProtocolTransport:
thrift.TMemoryBuffer does not implement thrift.TTransport (Close method has pointer receiver)

I am unsure of how to fix this, as TMemoryBuffer does not seem to implement TTransport and I can't find documentation of how TMemoryBuffer should be used instead.

  • 写回答

1条回答 默认 最新

  • dpvr49226 2017-11-22 03:26
    关注

    The important part of that error is Close method has pointer receiver.

    A function can be defined on a type, or a pointer to that type (i.e a pointer receiver), the TTransport interface defines a function Close with a pointer receiver.

    Read the tour of go for a refresher on pointer receivers.

    Changing your code to the following, should work:

    transport := &thrift.TMemoryBuffer{
        Buffer: bytes.NewBuffer(data),
    }
    

    One way to think about the problem would be that thrift.TMemoryBuffer does not have Close function defined on it, but that *thrift.TMemoryBuffer does. And the function NewTBinaryProtocolTransport requires a type with a Close function defined as specified by the interface.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部