go grpc:无法导入github.com/golang/protobuf/proto(没有必需的模块提供包“github.com/golang/protobuf/proto”)

lsmepo6l  于 2023-09-28  发布在  Go
关注(0)|答案(1)|浏览(81)

proto文件正在导入“github.com/golang/protobuf/proto”,而不是“google.golang.org/protobuf/proto”,当“protoc --proto_path=proto proto/*.proto --go_out=plugins=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

我的proto文件

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/ email protected(https://stackoverflow.com/cdn-cgi/l/email-protection)
$ go install google.golang.org/grpc/cmd/ email protected(https://stackoverflow.com/cdn-cgi/l/email-protection)
在安装新软件包之前输入go clean -modcache,并使用注解@latest重新安装最新版本

go版本:go版本go1.21.1 Linux/amd 64 on Ubuntu 20.4
protoc --versionlibprotoc 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@latest安装了新的二进制文件。它仍然使用旧的导入生成go文件

Edit 2:找不到protoc-gen-go --version,但protoc-gen-go-grpc --version为1.2.0。protoc --版本是libprotoc 3.6.1,其中是protoc-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

goucqfw6

goucqfw61#

正如@puellanivis在https://github.com/golang/protobuf/issues/1451中提到的
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

相关问题