君子之藏 發表於 2021-12-10 21:06:00

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

<h2 id="personproto文件">person.proto文件</h2>
<pre><code>//指定版本
//注意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;
}
</code></pre>
<h2 id="详解">详解</h2>
<pre><code>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代码输出目录
最后一个参数表示源文件

</code></pre>
<h2 id="grpc引起错误">grpc引起错误</h2>
<p>proto文件中如果没有添加option go_package = "/proto";这行会报下面这种错误。</p>
<pre><code>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.
</code></pre>
<p>原因是protoc-gen-go的不同版本兼容性问题。</p>
<p>解决办法:<br>
一是,在proto文件中加上option go_package = "/proto";<br>
二是采用老版本的proto-gen-go,使用命令切换为v1.3.2版本 go get -u github.com/golang/protobuf/protoc-gen-go@v1.3.2</p>
<p>原文链接:https://blog.csdn.net/weixin_43851310/article/details/115431651</p>


</div>
<div id="MySignature" role="contentinfo">
    写入自己的博客中才能记得长久<br><br>
来源:https://www.cnblogs.com/heris/p/15673865.html
頁: [1]
查看完整版本: --go_out: protoc-gen-go: Plugin failed with status code 1.