我有一些使用谷歌原型的代码。这些是源文件:
原始文件:
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);
}编译后的go文件:
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)
}当我最后尝试像这样使用编译后的服务时:
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{})
...
}我得到以下错误:
不能使用“"myproject/vendor/github.com/golang/protobuf/ptypes/empty".Empty) {}”(键入"myproject/vendor/github.com/golang/protobuf/ptypes/empty".*google_protobuf.Empty类型为*google_protobuf.Empty{})
它们是相同的(它们甚至解析到同一个文件)。我在这里错过了什么?
发布于 2017-12-13 12:05:12
您的错误出现在以下一行:
_, err = sink.Flush(context.Background(), *google_protobuf.Empty{})*google_protobuf.Empty{}试图取消对结构的引用,但是您的函数原型需要一个指向google_protobuf.Empty的指针。使用&google_protobuf.Empty{}代替。当您以真正的数据结构而不是空的数据结构结束时,您可能会按照以下的方式来做一些事情:
req := google_protobuf.MyRequestStruct{}
_, err = service.Method(context.Background(), &req)有关go中指针语法的概述,请使用巡演
https://stackoverflow.com/questions/47792443
复制相似问题