--go_out: protoc-gen-go: Plugin failed with status code 1.

person.proto文件

//指定版本
//注意proto3与proto2的写法有些不同
syntax = "proto3";
 
//包名,通过protoc生成时go文件时
option go_package="/address2";
 
//手机类型
//枚举类型第一个字段必须为0
enum PhoneType {
    HOME = 0;
    WORK = 1;
}
 
//手机
message Phone {
    PhoneType type = 1;
    string number = 2;
}
 
//人
message Person {
    //后面的数字表示标识号
    int32 id = 1;
    string name = 2;
    //repeated表示可重复
    //可以有多个手机,列表类型
    repeated Phone phones = 3;
}
 
//联系簿
message ContactBook {
    repeated Person persons = 1;
}

详解

rotoc -I=./proto --go_out=. ./proto/*
protoc -I=$SRC_DIR --go_out=$DST_DIR $SRC_DIR/addressbook.proto
结果:
在/proto目录里生成了helloworld.pb.go文件
这里option go_package 定义了导入的路径/proto,而–go_out也定义了路径,所有最后令–go_out=.


参数
-I:源文件的目录(可省略)
--go_out: 设置所生成的Go代码输出目录
最后一个参数表示源文件

grpc引起错误

proto文件中如果没有添加option go_package = "/proto";这行会报下面这种错误。

protoc-gen-go: unable to determine Go import path for "proto/helloworld.proto"

Please specify either:
        • a "go_package" option in the .proto source file, or
        • a "M" argument on the command line.

See https://developers.google.com/protocol-buffers/docs/reference/go-generated#package for more information.

--go_out: protoc-gen-go: Plugin failed with status code 1.

原因是protoc-gen-go的不同版本兼容性问题。

解决办法:
一是,在proto文件中加上option go_package = "/proto";
二是采用老版本的proto-gen-go,使用命令切换为v1.3.2版本 go get -u github.com/golang/protobuf/protoc-gen-go@v1.3.2

原文链接:https://blog.csdn.net/weixin_43851310/article/details/115431651

posted @ 2021-12-10 21:06  ty1539  阅读(6259)  评论(0编辑  收藏  举报