protoc生成go文件命令错误
写完rpc service后 用 protoc -I=. --go_out=plugins=grpc,paths=source_relative:gen/go trip.proto 生成过文件报错了--go_out: protoc-gen-go: plugins are not supported; use 'protoc --go-grpc_out=...' to generate gRPC
按照提示 我把命令换成了 protoc --go-grpc_out=gen/go --go-grpc_opt=paths=source_relative trip.proto 但是这个命令生成的go文件 里边有service 没有message
trip_grpc.pb.go:
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
package trippb
import (
context "context"
grpc "google.golang.org/grpc"
codes "google.golang.org/grpc/codes"
status "google.golang.org/grpc/status"
)
// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
// Requires gRPC-Go v1.32.0 or later.
const _ = grpc.SupportPackageIsVersion7
// TripServiceClient is the client API for TripService service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
type TripServiceClient interface {
GetTrip(ctx context.Context, in *GetTripRequest, opts ...grpc.CallOption) (*GetTripResponse, error)
}
type tripServiceClient struct {
cc grpc.ClientConnInterface
}
func NewTripServiceClient(cc grpc.ClientConnInterface) TripServiceClient {
return &tripServiceClient{cc}
}
func (c *tripServiceClient) GetTrip(ctx context.Context, in *GetTripRequest, opts ...grpc.CallOption) (*GetTripResponse, error) {
out := new(GetTripResponse)
err := c.cc.Invoke(ctx, "/coolcar.TripService/GetTrip", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// TripServiceServer is the server API for TripService service.
// All implementations must embed UnimplementedTripServiceServer
// for forward compatibility
type TripServiceServer interface {
GetTrip(context.Context, *GetTripRequest) (*GetTripResponse, error)
mustEmbedUnimplementedTripServiceServer()
}
// UnimplementedTripServiceServer must be embedded to have forward compatible implementations.
type UnimplementedTripServiceServer struct {
}
func (UnimplementedTripServiceServer) GetTrip(context.Context, *GetTripRequest) (*GetTripResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method GetTrip not implemented")
}
func (UnimplementedTripServiceServer) mustEmbedUnimplementedTripServiceServer() {}
// UnsafeTripServiceServer may be embedded to opt out of forward compatibility for this service.
// Use of this interface is not recommended, as added methods to TripServiceServer will
// result in compilation errors.
type UnsafeTripServiceServer interface {
mustEmbedUnimplementedTripServiceServer()
}
func RegisterTripServiceServer(s grpc.ServiceRegistrar, srv TripServiceServer) {
s.RegisterService(&TripService_ServiceDesc, srv)
}
func _TripService_GetTrip_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(GetTripRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(TripServiceServer).GetTrip(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/coolcar.TripService/GetTrip",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(TripServiceServer).GetTrip(ctx, req.(*GetTripRequest))
}
return interceptor(ctx, in, info, handler)
}
// TripService_ServiceDesc is the grpc.ServiceDesc for TripService service.
// It's only intended for direct use with grpc.RegisterService,
// and not to be introspected or modified (even as a copy)
var TripService_ServiceDesc = grpc.ServiceDesc{
ServiceName: "coolcar.TripService",
HandlerType: (*TripServiceServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "GetTrip",
Handler: _TripService_GetTrip_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "trip.proto",
}
111
收起
正在回答 回答被采纳积分+1
1回答
浅弋璃鱼
2021-08-14 14:48:11
我也遇到了, 看官方文档: https://grpc.io/docs/languages/go/quickstart/#regenerate-grpc-code
在`server/proto/`目录下执行命令改成:
protoc -I=. --go_out=paths=source_relative:gen/go --go-grpc_out=. --go-grpc_opt=paths=source_relative trip.proto
但是生成trip_grpc.pb.go文件的路径在`server/proto`, 需要手动移动到`/gen/go/`下; 如果你使用了git, 推荐使用git相关命令
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星