go grpc: cannot import github.com/golang/protobuf/proto (no required module provides package 'github.com/golang/protobuf/proto')

王林
Release: 2024-02-09 09:00:11
forward
1148 people have browsed it

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

php editor Zimo encountered an error when using go grpc, prompting that the "github.com/golang/protobuf/proto" module could not be imported. This error is usually caused by missing required modules. Before using go grpc, we need to ensure that the protobuf library has been correctly installed and the relevant proto packages have been correctly imported in the code. Next, I will introduce in detail how to solve this problem.

Question content

When "protoc --proto_path=proto proto/*.proto --go_out=plugins", the proto file is importing "github.com/golang/protobuf/proto" Instead of "google.golang.org/protobuf/proto" =grpc:pb"command

Importing files

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
Copy after login

My prototype file

syntax="proto3";

message Processor{

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

~go/bin/protoc-gen-go-grpc has version

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
Copy after login

what did I do

Initial installation

$ go install google.golang.org/protobuf/cmd/[email protected]

$ go install google.golang.org/grpc/cmd/[email protected]

Enter go clean -modcache before installing new packages and reinstall the latest version using the comment @latest

go version: go version on Ubuntu 20.4 go1.21.1 linux/amd64

Protocol--Version libprotoc 3.6.1

Using apt to install protobuf-compiler and golang-goprotobuf

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

I think the problem is here but I don't know what to fix or how to read this

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)
Copy after login

Edit: I think I originally installed it using go get -u github.com/golang/protobuf/proto but I removed it using rm -rf $(go env GOPATH)/pkg/mod/github.com/golang binaries/protobuf/proto and install the new version using go install google.golang.org/protobuf/cmd/protoc-gen-go@latest and go install google.golang.org/grpc/cmd/protoc-gen-go- grpc@latest. It still generates go files using the old imports

Edit2: protoc-gen-go --version not found, but protoc-gen-go-grpc --version is 1.2.0. protoc --The version is libprotoc 3.6.1 whereis protocol-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

Solution

As//m.sbmmt.com/link/a5481cd6d7517aa3fc6476dc7d9019ab<中提到的/a> Author: @puellanivis

The

$PATH variable in a Linux environment should start with /home/{username}/go/bin and then /usr/bin in sequence beginning. This is because we need to find google.golang.org/gprc/cmd/protoc-gen-go-grpc@latest before /usr/bin/protoc-gen-go.

Edit the ~/.bashrc or ~/.bash_profile file ($vim ~/.bashrc) and manually export the entire path environment. In my case I had to add

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
Copy after login

The above is the detailed content of go grpc: cannot import github.com/golang/protobuf/proto (no required module provides package 'github.com/golang/protobuf/proto'). For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!