dongshun1884 2017-12-13 11:57
浏览 163
已采纳

Protobuf无法识别相同的软件包

I have some code that uses google protobuf. These are the source files:

Proto file:

syntax = "proto3";

package my_package.protocol;
option go_package = "protocol";

import "github.com/golang/protobuf/ptypes/empty/empty.proto";

...

service MyService {
    rpc Flush        (google.protobuf.Empty) returns (google.protobuf.Empty);
}

Compiled go file:

package protocol

import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
import google_protobuf "github.com/golang/protobuf/ptypes/empty"

import (
    context "golang.org/x/net/context"
    grpc "google.golang.org/grpc"
)

...

type MyServiceClient interface {
    Flush(ctx context.Context, in *google_protobuf.Empty, opts ...grpc.CallOption) (*google_protobuf.Empty, error)
}

And when I finally try to use the compiled service like this:

import (
    "golang.org/x/net/context"

    pb "myproject/protocol"

    google_protobuf "github.com/golang/protobuf/ptypes/empty"
)
...
func Flush(sink pb.MyServiceClient) {
    _, err = sink.Flush(context.Background(), *google_protobuf.Empty{})
    ...
}

I get the following error:

Cannot use '*google_protobuf.Empty{}' (type "myproject/vendor/github.com/golang/protobuf/ptypes/empty".Empty) as type "myproject/vendor/github.com/golang/protobuf/ptypes/empty".*google_protobuf.Empty

Which are the same thing (they even resolve to the same file). What am I missing here?

  • 写回答

1条回答 默认 最新

  • douzhunlan5930 2017-12-13 12:05
    关注

    Your error is on this line:

     _, err = sink.Flush(context.Background(), *google_protobuf.Empty{})
    

    *google_protobuf.Empty{} is attempting to dereference the struct, but your function prototype expects a pointer to a google_protobuf.Empty. Use &google_protobuf.Empty{} instead. And when you end up with a real data structure rather than empty, you will probably do something along the lines of:

      req := google_protobuf.MyRequestStruct{}
      _, err = service.Method(context.Background(), &req)
    

    For an overview of pointer syntax in go, please take the tour

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

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)