我使用具有模块支持的Golang 1.11,因此我的项目未放入$GOPATH

我想编译proto文件,

我的档案结构

enter image description here

我的TaskInfo.proto

1
2
3
4
5
6
7
8
9
10
11
12
13
14
syntax ="proto3";
package chaochaogege.filecatcher.common;

option go_package ="common";

import"chaochaogege.com/filecatcher/common/ChunkInfo.proto";

message TaskInfo{
    string name = 1;
    string storePath = 2;
    uint32 workersNum = 3;
    uint32 totalSize = 4;
    repeated chaochaogege.filecatcher.common.ChunkInfo chunkInfo = 5;
}

ChunkInfo.proto

1
2
3
4
5
6
7
8
9
10
11
12
13
syntax ="proto3";
package chaochaogege.filecatcher.common;

option go_package ="common";

message ChunkInfo{
    uint32 startBytes = 1;
    uint32 endBytes = 2;
    uint32 totalBytes = 3;
    uint32 downloaded = 4;
    uint32 orderId = 5;

}

go.mod

1
2
3
4
5
module chaochaogege.com/filecatcher

require (
  github.com/golang/protobuf v1.2.0
)

当我运行时(在filecatcher / common目录中)

1
protoc --go_out=./ TaskInfo.proto

协议说:

1
2
chaochaogege.com/filecatcher/common/ChunkInfo.proto: File not found.TaskInfo.proto: Import"chaochaogege.com/filecatcher/common/ChunkInfo.proto" was not found or had errors.
TaskInfo.proto:13:14:"chaochaogege.filecatcher.common.ChunkInfo" is notdefined.

我已经用谷歌搜索了,但是所有问题都不是关于go模块的

我使用了错误的导入路径还是我的配置不正确?

如果Stackoverflow无法解决我的问题,我认为这是一个错误。
我应该去Github问题报告。


看起来您正在混淆路径

您的导入路径是

import"chaochaogege.com/filecatcher/common/ChunkInfo.proto";

如果要从filecatcher / common内部运行protoc,则希望将导入缩短为import"ChunkInfo.proto"并运行您现在尝试运行的命令。 会的。

但是,如果您想保持导入语句的原样,则必须导航到chaochaogege.com的父目录,并从此处运行以下命令,如下所示:

protoc -I chaochaogege.com/filecatcher/common/ chaogege.com/filecatcher/common/ChunkInfo.proto --go_out=chaochaogege.com/filecatcher/common/

  • 是的,您是对的,我必须在相对于导入路径的根目录中运行cmd。 谢谢你给我主意!