I am not able to call a gRPC function due to type mismatch
my proto file :
message Analytics {
fields ...
}
message AnalyticsSet {
repeated Analytics analytics = 1;
}
service StatService {
rpc MyMethod(AnalyticsSet) returns (<something>) {}
}
Now, I need to call "MyMethod"
My current code :
type Analytics struct {
same fields as in proto : Analytics
}
analytics := make([]Analytics, 4)
// .. some modifications in analytics ...
_, err := c.MyMethod(context.Background(), analytics)
if err != nil {
log.Fatalf("error: %s", err)
}
in Proto file "AnalyticsSet" is the array of "Analytics" and in Go code "analytics" is an array of type "Analytics" but this is not enough to call "MyMethod", and I am facing type mismatch..
How should I modify the go code ?
CodePudding user response:
You must use the Analytics
struct generated from the proto file -- you cannot use your own type.
You can generate the required Go code using protoc
with your .proto
file. Here is an example with gRPC generation options set as well:
.
$ protoc --go_out=. --go-grpc_out=. --go_opt=paths=source_relative --go-grpc_opt=paths=source_relative analytics.proto
Your proto file should have the go_package
option set to describe the Go import path for that your generated proto code belongs to. You will also need to install the go / go-grpc generator utilities required by protoc
:
$ go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
$ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
More details can be found in: