首页 文章

使用带模块的Golang1.11时,Protobuf导入“找不到文件”

提问于
浏览
0

我使用Golang 1.11和模块支持,所以我的项目是 not 放入 $GOPATH

我想编译 proto 文件,

我的文件结构

enter image description here


我的 TaskInfo.proto

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

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

module chaochaogege.com/filecatcher

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

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

protoc --go_out=./ TaskInfo.proto

protoc说:

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.

我用谷歌搜索,但所有的问题都不是去模块

我使用错误的导入路径或我的配置不正确吗?

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

1 回答

  • 1

    看起来你正在混淆路径

    你的导入路径是

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

    如果你想从filecatcher / common中运行protoc,那么你可能希望将导入缩短为 import "ChunkInfo.proto" 并运行你现在尝试运行的命令 . 它会工作 .

    但是如果你想保持你的import语句,那么你必须导航到chaochaogege.com的父目录并从那里运行以下命令,如下所示:

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

相关问题