登录
首页 >  Golang >  Go问答

无法导入 Golang Protobuf 的 Proto 包:缺少必需的模块

来源:stackoverflow

时间:2024-02-17 18:33:28 110浏览 收藏

从现在开始,我们要努力学习啦!今天我给大家带来《无法导入 Golang Protobuf 的 Proto 包:缺少必需的模块》,感兴趣的朋友请继续看下去吧!下文中的内容我们主要会涉及到等等知识点,如果在阅读本文过程中有遇到不清楚的地方,欢迎留言呀!我们一起讨论,一起学习!

问题内容

当“protoc --proto_path=proto proto/*.proto --go_out=plugins”时,proto 文件正在导入“github.com/golang/protobuf/proto”而不是“google.golang.org/protobuf/proto” =grpc:pb"命令

导入文件

import (
    fmt "fmt"
    proto "github.com/golang/protobuf/proto"
    math "math"
)
...
> This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package

我的原型文件

syntax="proto3";

message Processor{

    string name=1;
    uint32 cores=2;
    uint32 min_ghz=3;
    uint32 max_ghz=4; 
}

~go/bin/protoc-gen-go-grpc 有版本

go: downloading google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.3.0
go: downloading google.golang.org/grpc v1.58.2
go: downloading google.golang.org/protobuf v1.28.1

我做了什么

初始安装

$ go install google.golang.org/protobuf/cmd/[电子邮件受保护]

$ go install google.golang.org/grpc/cmd/[电子邮件受保护]

在安装新软件包之前输入go clean -modcache,并使用注释@latest重新安装最新版本

go 版本:Ubuntu 20.4 上的 go 版本 go1.21.1 linux/amd64

协议--版本 libprotoc 3.6.1

使用 apt 安装了 protobuf-compiler 和 golang-goprotobuf

sudo apt install protobuf-compiler
sudo apt install golang-goprotobuf -dev
export PATH="$PATH:$(go env GOPATH)/bin"

我觉得问题就在这里,但我不知道要修复什么或如何阅读此内容

go mod graph | grep github.com/golang/protobuf

example-first github.com/golang/[email protected]
github.com/golang/[email protected] github.com/google/[email protected]
github.com/golang/[email protected] google.golang.org/[email protected]
google.golang.org/[email protected] github.com/golang/[email protected]
google.golang.org/[email protected] github.com/golang/[email protected]
github.com/golang/[email protected] github.com/google/[email protected]
github.com/golang/[email protected] google.golang.org/[email protected]


go mod why github.com/golang/protobuf

go: downloading github.com/golang/protobuf v1.5.3
go: downloading github.com/google/go-cmp v0.5.5
go: downloading golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543
# github.com/golang/protobuf
(main module does not need package github.com/golang/protobuf)

编辑:我想我最初使用 go get -u github.com/golang/protobuf/proto 安装了它,但我使用 rm -rf $(go env GOPATH)/pkg/mod/github.com/golang 删除了二进制文件/protobuf/proto 并使用 go install google.golang.org/protobuf/cmd/protoc-gen-go@latest 安装新版本并 go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@最新的。它仍然使用旧导入生成 go 文件

Edit2:找不到 protoc-gen-go --version,但 protoc-gen-go-grpc --version 是 1.2.0。 protoc --版本是 libprotoc 3.6.1 whereis 协议-gen-go protoc-gen-go:/usr/bin/protoc-gen-go /home/hp/go/bin/protoc-gen-go /usr/share/man/man1/protoc-gen-go.1.gz p>

正确答案


正如https://github.com/golang/protobuf/issues/1451 作者:@puellanivis

Linux 环境中的 $PATH 变量应以 /home/{username}/go/bin 开头,然后按顺序以 /usr/bin 开头。这是因为我们需要在 /usr/bin/protoc-gen-go 之前找到 google.golang.org/gprc/cmd/protoc-gen-go-grpc@latest

编辑 ~/.bashrc~/.bash_profile 文件 ($vim ~/.bashrc) 并手动导出整个路径环境。就我而言,我必须添加

export PATH=/home/hp/go/bin:/usr/local/go:/home/hp/go:usr/local/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

好了,本文到此结束,带大家了解了《无法导入 Golang Protobuf 的 Proto 包:缺少必需的模块》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

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