解决:Import googleapiannotations.proto was not found or had errors

解决:Import “google/api/annotations.proto” was not found or had errors.

一、问题概述

在使用protoc命令的时候,很可能会出现这样的问题。因为proto文件中有类似于import "google/api/annotations.proto";这样的导包操作,protoc命令默认会导入同级目录下的proto文件,所以,如果你import的是同级目录下的proto文件一般不会出现was not found or had errors.的问题

二、解决办法

既然是找不到我们要importproto文件,那我们就通过在命令中添加参数去指定好。

打开protoc帮助文档

protoc -h

会出现下面的内容:

Parse PROTO_FILES and generate output based on the options given:
  -IPATH, --proto_path=PATH   Specify the directory in which to search for
                              imports.  May be specified multiple times;
                              directories will be searched in order.  If not
                              given, the current working directory is used.
                              If not found in any of the these directories,
                              the --descriptor_set_in descriptors will be
                              checked for required proto file.
## ...下面的省略

百度翻译一下:

指定要在其中搜索导入的目录。 可以多次指定;将按顺序搜索目录。
如果未给出,则使用当前工作目录。
如果在这些目录中找不到,将检查他 --descriptor_set_in 描述符以查找所需的原型文件。

大概意思就是可以通过这个命令指定要搜索的目录,你需要的importprotoc文件可以通过这个命令指定,这个命令的简写是-I,下面将介绍如何使用

三、示例

就比如我自己遇到的这个问题:

 Import "google/api/annotations.proto" was not found or had errors.

通过查阅资料,得知可以通过如下方式下载得到对应的protoc文件:

1、下载mod

go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway@v1.14.5

之所以下载这个,是因为在github.com\grpc-ecosystem\grpc-gateway@v1.14.5\third_party\googleapis\google\api目录下就有我们需要的annotations.proto文件。

执行完上述命令后,就会将protoc-gen-grpc-gateway下载到电脑的GOPATH下,自己电脑的GOPATH可以通过命令go env查看。

在这里,本人的是Windows系统, GOPATH=C:\Users\Administrator\go

通过浏览GOPATH目录,知道了我们import"google/api/annotations.proto"

所在的路径是C:\Users\Administrator\go\pkg\mod\github.com\grpc-ecosystem\grpc-gateway@v1.14.5\third_party\googleapis,换句话说也就是$GOPATH\pkg\mod\github.com\grpc-ecosystem\grpc-gateway@v1.14.5\third_party\googleapis,知道了这个后就可以进行下面的步骤了

2、命令中指定依赖:

注意: 路径中要用/ !!!

protoc -I $GOPATH/pkg/mod/github.com/grpc-ecosystem/grpc-gateway@v1.14.5/third_party/googleapis \
--go_out=plugins=grpc:. ./*.proto
  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

生命中有太多不确定

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值