首页 >后端开发 >Golang >如何解决 Protobuf 导入错误:找不到'google/api/annotations.proto”?

如何解决 Protobuf 导入错误:找不到'google/api/annotations.proto”?

Barbara Streisand
Barbara Streisand原创
2024-11-05 20:59:02459浏览

How to Resolve Protobuf Import Errors: Can't Find

Protobuf 导入错误疑难解答:添加“google/api/annotations.proto”作为依赖项

按照文档设置时gRPC 网关,用户可能会遇到与“google/api/annotations.proto”相关的导入错误。本文提供了如何解决此依赖问题的指导。

将以下行添加到代码中时会发生错误:

import "google/api/annotations.proto";

正如文档提到的,用户需要提供“protoc”编译器需要第三方 protobuf 文件。但具体的方法并没有明确说明。本文旨在阐明添加“google/api/annotations.proto”作为依赖项的过程。

解决方案:

解决此问题的一种方法是将所需的第三方 Google API 及其内容添加到项目的根目录中。文档认为此方法是可接受的。

实现:

  1. 从 GitHub 存储库下载 Google API protobuf 文件:https://github.com /googleapis/googleapis/tree/main/google/api
  2. 解压下载的文件,并将包含 protobuf 文件的“google/api”目录复制到项目的根目录。
  3. 确保您已安装“protoc”编译器并在您的 PATH 中可用。
  4. 照常编译您的原型文件,确保包含对 Google API 的依赖项:
protoc --proto_path=<path_to_google_api_proto_files> --proto_path=<path_to_my_proto_files> <my_proto_files.proto>

此方法将成功解决依赖错误并允许您继续生成 gRPC 网关。

以上是如何解决 Protobuf 导入错误:找不到'google/api/annotations.proto”?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn