栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Go语言

使用protoc编译时出现Import “google/protobuf/any.proto“ was not found or had errors.问题的一个解决方法

Go语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用protoc编译时出现Import “google/protobuf/any.proto“ was not found or had errors.问题的一个解决方法

使用protoc编译时出现Import “google/protobuf/any.proto” was not found or had errors.问题的解决方法

在windows环境下使用golang中的protobuf时,在自定义的.proto文件中引入了外部的proto文件

syntax = "proto3";

import "google/protobuf/any.proto";
package proto;

在使用命令

protoc --go_out=plugins=grpc:. ./proto/*.proto

进行编译时,出现错误

proto/common.proto:3:1: Import "google/protobuf/any.proto" was not found or had errors.

一个简单粗暴但方便理解的方法如下:

首先去https://github.com/protocolbuffers/protobuf下载src/google/protobuf,接着将google文件复制后移动至项目下,项目的结构下所示

—Project
	—Proto
		—test.proto
	—google
		—protobuf
			—...

test.proto中引入了外部的proto,接着在在命令行中使得当前位置为 Project> ,即没有进入任何一个子目录,然后正常使用

protoc --go_out=plugins=grpc:. ./proto/*.proto

就可以正常编译,当然这是非常麻烦的方法

还有一个更灵活的方式是使用proto_path指定要导入的文件的位置,首先先确定GOPATH的位置,我把google/protobuf放在了GOPATH/src目录下,当前的文件结构如下

—GOPATH
	—src
		—google
			—protobuf
	—Project
		—Proto
			—test.proto	

因此先保证当前位置为 Project> ,接着使用如下命令即可正常编译

protoc -I ../src -I ./ --go_out=plugins=grpc:. ./proto/*.proto
#-I就是proto_path的缩写表示

当然如果你的项目路径跟我不一致,也可以使用绝对路径,假如你的google/protobuf的路径格式在windows下为

E:GoCodesrcgoogleprotobuf

那么你需要将命令改为

protoc -I E:GoCodesrc -I ./ --go_out=plugins=grpc:. ./proto/*.proto
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/937819.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号