导入 "google/api/annotations.proto" 未找到或有错误。如何将其添加为依赖项?

Import "google/api/annotations.proto" was not found or had errors. How do I add it as a dependency?

按照 how to set up a gRPC gateway 上的文档,我发现自己卡在了生成 grpc 网关的第四步。

也就是说,当添加以下行时,事情就会崩溃:

import "google/api/annotations.proto";

文档说 You will need to provide the required third party protobuf files to the protoc compiler - 但实际上并没有这样做。

如何将 google/api/annotations.proto 添加为依赖项?

我通过将 third party google apis 及其内容添加到我的项目的根目录来解决它。

感觉不对,但显然是这样is encouraged

我只复制 annotations.proto 和 http.proto 就解决了 在主要协议中:

import "Proto/google/api/annotations.proto";

在annotations.proto

里面
import "Proto/google/api/http.proto";

我的文件夹如下所示:

我有同样的问题,我按照这个结构解决了它:

proto
├── google
│   └── api
│       ├── annotations.proto
│       └── http.proto
└── helloworld
    └── hello_world.proto

和运行命令:

protoc -I ./proto \
   --go_out ./proto --go_opt paths=source_relative \
   --go-grpc_out ./proto --go-grpc_opt paths=source_relative \
   --grpc-gateway_out ./proto --grpc-gateway_opt paths=source_relative \
   ./proto/helloworld/hello_world.proto

If you are using protoc to generate stubs, you need to ensure the required dependencies are available to the compiler at compile time. These can be found by manually cloning and copying the relevant files from the googleapis repository, and providing them to protoc when running. The files you will need are:

google/api/annotations.proto
google/api/field_behaviour.proto
google/api/http.proto
google/api/httpbody.proto

来自 grpc-gateway

例如项目根目录中的运行 git submodule add https://github.com/googleapis/googleapis 获取实际版本

protoc 版本将这些 well-known typesbin(在 Linux 上)一起包含在一个目录(libinclude)中。

您不需要克隆|将它们复制到任何地方(其他),IIRC protoc 甚至不需要单独的 proto_path 目录标志来为导入的原型生成代码他们

Google 还为众所周知的类型提供包作为 Golang Protobuf SDK 的一部分:

https://pkg.go.dev/google.golang.org/protobuf/types/known

可以这么说,在 Linux 上,按原样使用 protoc 版本,您应该能够在不更改 protoc 命令和 [=18] 的情况下为这些类型添加导入=]