douchixu3686 2017-11-22 09: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 11: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.

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

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler