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

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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀