使用dep时,如何正确为协议添加golang protobuf/ptypes? [英] How do I correctly include golang protobuf/ptypes for protoc when using dep?
问题描述
我遇到了麻烦,包括 google/protobuf/timestamp.proto 众所周知的类型,当使用 dep 时.
I'm having trouble including the google/protobuf/timestamp.proto well known type, when using dep.
我收到一个错误:google/protobuf/timestamp.proto: File not found
service.proto:
service.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的软件包:
Install package containing timestamp .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定义的目录存在:
The dir containing timestamp's .proto definition exists:
file vendor/github.com/golang/protobuf/ptypes/timestamp/timestamp.proto
vendor/github.com/golang/protobuf/ptypes/timestamp/timestamp.proto: ASCII text
我要在本地安装协议,因为我不想将协议版本锁定/绑定到该项目:
I'm installing protoc locally because I wan't to lock-in/tie a protoc version to this project:
# 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
路径有关.
The protoc
error you are getting relates to your INCLUDE
path.
安装protoc
编译器(例如,安装到/usr/local/bin/protoc
)时,要使用它来获取Google的标准原型定义(例如timestamp.proto
),这些定义必须沿着INCLUDE
路径添加(在MacOS/Linux上)一个可以使用/usr/local/include
).注意:protoc标头应该已经包含在protoc编译器中.
When you installed the protoc
compiler (e.g. to /usr/local/bin/protoc
), for it to pick up google's standard proto definitions like timestamp.proto
- these need to be added somewhere along your INCLUDE
path (on MacOS/Linux one may use /usr/local/include
). Note: the protoc headers should have been included with the protoc compiler.
因此,原型导入文件通常位于以下位置:
So your proto import file would typically be located here:
/usr/local/include/google/protobuf/timestamp.proto
当protoc
编译器看到如下所示的导入时,将引用此路径:
This path will be referenced when the protoc
compiler sees an import like:
import "google/protobuf/timestamp.proto";
因此,请检查您的INCLUDE
路径,并确保正确安装了protoc
标头.
So check your INCLUDE
path and ensure the protoc
headers are installed correctly.
这篇关于使用dep时,如何正确为协议添加golang protobuf/ptypes?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!