一分耕耘,一分收获!既然打开了这篇文章《使用 dep 时如何正确包含 protoc 的 golang protobuf/ptypes ?》,就坚持看下去吧!文中内容包含等等知识点...希望你能在阅读本文后,能真真实实学到知识或者帮你解决心中的疑惑,也欢迎大佬或者新人朋友们多留言评论,多给建议!谢谢!
使用 dep 时,我在包含 google/protobuf/timestamp.proto 众所周知的类型时遇到问题。
我收到错误:google/protobuf/timestamp.proto:文件未找到
服务.proto:
syntax = "proto3";
import "google/protobuf/timestamp.proto";
package com.rynop.platform;
option go_package = "rpc";
service platformservice {
rpc test(emptymessage) returns (emptymessage);
}
message emptymessage { }
message a {
string summary = 1;
google.protobuf.timestamp start = 2;
}
message b {
repeated a foos = 1;
}
安装包含时间戳.proto def的包:
dep ensure -add github.com/golang/protobuf/ptypes/timestamp
运行 protoc 并得到错误:
build/bin/protoc -ivendor -i. --twirp_typescript_out=version=v6:./clients/ts-json/ rpc/service.proto google/protobuf/timestamp.proto: file not found.
包含时间戳的 .proto 定义的目录存在:
file vendor/github.com/golang/protobuf/ptypes/timestamp/timestamp.proto vendor/github.com/golang/protobuf/ptypes/timestamp/timestamp.proto: ascii text
我在本地安装 protoc,因为我不想将 protoc 版本锁定/绑定到该项目:
# fetch the protoc compiler
OS_NAME=$(shell uname -s)
ifeq ($(OS_NAME),Darwin)
PROTOC_URL=https://github.com/google/protobuf/releases/download/v3.7.1/protoc-3.7.1-osx-x86_64.zip
endif
ifeq ($(OS_NAME),Linux)
PROTOC_URL=https://github.com/google/protobuf/releases/download/v3.7.1/protoc-3.7.1-linux-x86_64.zip
endif
build/protoc build/bin/protoc:
mkdir -p build/protoc/bin build/bin
cd build/protoc && curl -L -o protoc.zip $(PROTOC_URL) && \
unzip protoc.zip && mv bin/protoc ../bin/protoc
我做错了什么?
解决方案
您收到的 protoc 错误与您的 include 路径有关。
当您安装 protoc 编译器(例如 /usr/local/bin/protoc)时,它可以获取 google 的标准原型定义,例如 timestamp.proto - 这些需要添加到您的 include 路径中的某个位置(在 macos 上) /linux 可以使用 /usr/local/include)。注意:protoc 标头应该已包含在 protoc 编译器中。
因此您的原型导入文件通常位于此处:
/usr/local/include/google/protobuf/timestamp.proto
当 protoc 编译器看到如下导入时,将引用此路径:
import "google/protobuf/timestamp.proto";
因此,请检查您的 include 路径并确保 protoc 标头安装正确。
今天关于《使用 dep 时如何正确包含 protoc 的 golang protobuf/ptypes ?》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!
本文内容来源于互联网,如有侵权请联系删除。
