我最近用一个现有的proto 3代码库更新了最新的protoc和Go插件,但在新的未实现的服务器功能上遇到了麻烦。用于Grpc服务器的结构已经嵌入了另一个接口,该接口描述了此服务实现的方法。在将UnimplementedServer引用嵌入到我的结构中之后,我从编译器那里得到了一个不明确的错误,它告诉我我不再实现我的服务方法了。我构建代码的方式是否存在一些问题?要使用libprotoc 3.17.3、protoc gen go v1.27.1和protoc gen go grpc 1.1.0复制的代码如下:api/proto/core\u服务。原型:
package testbed;
option go_package = "internal/proto";
service Core {
rpc CreateThing(Thing) returns (ThingResponse) {};
}
message Thing {
string name = 1;
}
message ThingResponse {
int64 result = 1;
}
内部/核心。转到:
import (
"context"
"testbed.org/demo/internal/proto"
)
type CoreApi interface {
CreateThing(ctx context.Context, t *proto.Thing) (*proto.ThingResponse, error)
}
内部/core_endpoints.go:
import (
"context"
"testbed.org/demo/internal/proto"
)
type CoreEndpoints struct {}
func (ce CoreEndpoints) CreateThing(_ context.Context, _ *proto.Thing) (*proto.ThingResponse, error) {
return nil, nil
}
命令/服务。转到:
import (
//"context"
. "testbed.org/demo/internal"
"testbed.org/demo/internal/proto"
"google.golang.org/grpc"
)
type MyServer struct {
CoreApi
proto.UnimplementedCoreServer
}
func main() {
mySvr := &MyServer{CoreApi: &CoreEndpoints{}}
//_, _ = mySvr.CoreApi.CreateThing(context.Background(), &proto.Thing{})
grpcSvr := grpc.NewServer()
proto.RegisterCoreServer(grpcSvr, mySvr)
}
构建:
protoc -I api/proto/ api/proto/*.proto --go_out=. --go-grpc_out=.
go build -o bin/svc cmd/service.go
cmd/service.go:19:26: MyServer.CreateThing is ambiguous
cmd/service.go:19:26: cannot use mySvr (type *MyServer) as type "testbed.org/demo/internal/proto".CoreServer in argument to "testbed.org/demo/internal/proto".RegisterCoreServer:
*MyServer does not implement "testbed.org/demo/internal/proto".CoreServer (missing CreateThing method)
gmake: *** [Makefile:8: service] Error 2