go - 使用带模块的 Golang1.11 时 Protobuf 导入“找不到文件”
问题描述
我正在使用带有模块支持的 Golang 1.11,所以我的项目没有放入$GOPATH
我想编译proto
文件,
我的文件结构
我的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;
}
去.mod
module chaochaogege.com/filecatcher
require (
github.com/golang/protobuf v1.2.0
)
当我运行跟随(在 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.
我用谷歌搜索过,但所有问题都不是关于 go 模块的
我使用了错误的导入路径还是我的配置不正确?
如果 Stackoverflow 无法回答我的问题,我认为这是一个错误。我应该去 Github issue 报告..
解决方案
看起来你正在混淆路径
您的导入路径是
import "chaochaogege.com/filecatcher/common/ChunkInfo.proto";
如果您想从 filecatcher/common 内部运行 protoc,那么您需要将导入缩短为 justimport "ChunkInfo.proto"
并运行您现在尝试运行的命令。它会起作用的。
但是如果你想保持你的 import 语句保持原样,那么你必须导航到 chaochaogege.com 的父目录并从那里运行以下命令,如下所示:
protoc -I chaochaogege.com/filecatcher/common/ chaogege.com/filecatcher/common/ChunkInfo.proto --go_out=chaochaogege.com/filecatcher/common/
推荐阅读
- keras - 过度拟合、过度训练或不正确的架构
- javascript - 如果元素被隐藏,则跳过表单中的规则
- android - 在哪里可以找到系统映像以在 Android Studio 中创建虚拟设备
- python - Tensorflow:低级 LSTM 实现
- javascript - 如何在滤镜/切换效果上制作动画?
- racket - “如何设计程序”的问题,I 固定大小数据,1.7,练习 9
- mysql - Trigger optimisation
- mysql - SQL查询以显示列中字符串值的一部分
- java - 在 java 中读取属性文件的 NullPointerException
- java - 如何使用 JavaFX 应用程序的 sqlite 数据库文件正确构建 exe 文件?