2017-12-13 1 views
1

Ich habe einen Code, der Google Protobuf verwendet. Dies sind die Quelldateien:Go protobuf nicht identische Pakete zu erkennen

Proto-Datei:

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); 
} 

Zusammengestellt go-Datei:

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) 
} 

Und wenn ich versuche, endlich die kompilierte Dienst wie diese zu verwenden:

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{}) 
    ... 
} 

Ich erhalte den folgenden Fehler:

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

Welche sind das gleiche (sie sogar in die gleiche Datei auflösen). Was fehlt mir hier?

+0

Sie sind nicht das Gleiche, wie der Fehler deutlich zeigt. – Adrian

Antwort

2

Ihr Fehler ist auf dieser Linie:

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

*google_protobuf.Empty{} zu dereferenzieren der Struktur versucht, aber Ihre Funktion Prototyp erwartet einen Zeiger auf eine google_protobuf.Empty. Verwenden Sie stattdessen &google_protobuf.Empty{}. Und wenn Sie mit einem echten Datenstruktur am Ende nicht leer ist, werden Sie wahrscheinlich tun etwas entlang der Linien von:

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

Eine Übersicht der Zeiger Syntax in gehen, nehmen Sie bitte die tour

Verwandte Themen