簡體   English   中英

使用dep時,如何正確為協議添加golang protobuf / ptypes?

[英]How do I correctly include golang protobuf/ptypes for protoc when using dep?

使用dep時,包括google / protobuf / timestamp.proto眾所周知的類型在內,我遇到了麻煩。

我收到錯誤消息: google/protobuf/timestamp.proto: File not found

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;
}

安裝包含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定義的目錄:

file vendor/github.com/golang/protobuf/ptypes/timestamp/timestamp.proto
vendor/github.com/golang/protobuf/ptypes/timestamp/timestamp.proto: ASCII text

我要在本地安裝協議,因為我不想將協議版本鎖定/綁定到該項目:

# 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的標准proto定義(例如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頭。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM