登录
首页 >  Golang >  Go问答

使用基于 go 的项目(cortex)构建类似 protobuf(protoc)的方式

来源:stackoverflow

时间:2024-02-10 23:12:25 393浏览 收藏

从现在开始,努力学习吧!本文《使用基于 go 的项目(cortex)构建类似 protobuf(protoc)的方式》主要讲解了等等相关知识点,我会在golang学习网中持续更新相关的系列文章,欢迎大家关注并积极留言建议。下面就先一起来看一下本篇正文内容吧,希望能帮到你!

问题内容

我正在尝试按照此处的说明从此处找到的 protobuf 定义创建 python 类。

当我跑步时:

protoc -i=.  --python_out=target cortex.proto

我遇到了一个错误:

cortex.proto:7:1: Import "github.com/gogo/protobuf/gogoproto/gogo.proto" was not found or had errors.

我很清楚我还需要克隆依赖项。我使用 git 克隆 https://github.com/gogo/protobuf 手动完成了此操作,但随后陷入了依赖项的依赖关系。

我相信我需要做的是使用 go 为我提取全套依赖项。我尝试了 go get 但这会导致依赖项被克隆为名称如 ~/go/pkg/mod/github.com/gogo/[email protected] (包括标签)。结果protoc仍然找不到它们。

我想我错过了一些关于 go 如何工作的简单内容。我没有花太多时间在 go 上,所以我怀疑我只是没有找到正确的命令来以 protobuf 接受的方式提取依赖项。

如何从 go 项目编译 protobuf 文件(使用 go 依赖项)?


正确答案


在本例中,我错过的命令似乎是 go modvendor,这是我在答案 https://stackoverflow.com/a/65874584/453851 中发现的。该命令将下载模块复制到项目存储库根目录下的 vendor 文件夹中。

coretex.proto 构建为 python

在我安装了 goprotoc 的 mac 上:

git clone https://github.com/cortexproject/cortex.git
cd cortex/pkg/cortexpb
go get
go mod vendor

cd ../..  # Root of the cloned cortex project
mkdir python
# Don't forget to include the vendor directory
protoc -I=vendor -I=pkg --python_out=python pkg/cortexpb/cortex.proto
# You may need to compile others from vendor to get the python module working
protoc -I=vendor --python_out=python vendor/github.com/gogo/protobuf/gogoproto/gogo.proto

# Populate missing __init__.py files
find python -type d -exec touch {}/__init__.py ';'

这会在 python 目录中留下 python 类。

理论要掌握,实操不能落!以上关于《使用基于 go 的项目(cortex)构建类似 protobuf(protoc)的方式》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>