Table of Contents
Question content
Solution
Home Backend Development Golang go grpc: cannot import github.com/golang/protobuf/proto (no required module provides package 'github.com/golang/protobuf/proto')

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

Feb 09, 2024 am 09:00 AM

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

Ashttps://www.php.cn/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!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Go language pack import: What is the difference between underscore and without underscore? Go language pack import: What is the difference between underscore and without underscore? Mar 03, 2025 pm 05:17 PM

Go language pack import: What is the difference between underscore and without underscore?

How to implement short-term information transfer between pages in the Beego framework? How to implement short-term information transfer between pages in the Beego framework? Mar 03, 2025 pm 05:22 PM

How to implement short-term information transfer between pages in the Beego framework?

How do I write mock objects and stubs for testing in Go? How do I write mock objects and stubs for testing in Go? Mar 10, 2025 pm 05:38 PM

How do I write mock objects and stubs for testing in Go?

How to convert MySQL query result List into a custom structure slice in Go language? How to convert MySQL query result List into a custom structure slice in Go language? Mar 03, 2025 pm 05:18 PM

How to convert MySQL query result List into a custom structure slice in Go language?

How can I define custom type constraints for generics in Go? How can I define custom type constraints for generics in Go? Mar 10, 2025 pm 03:20 PM

How can I define custom type constraints for generics in Go?

How can I use tracing tools to understand the execution flow of my Go applications? How can I use tracing tools to understand the execution flow of my Go applications? Mar 10, 2025 pm 05:36 PM

How can I use tracing tools to understand the execution flow of my Go applications?

How to write files in Go language conveniently? How to write files in Go language conveniently? Mar 03, 2025 pm 05:15 PM

How to write files in Go language conveniently?

How do you write unit tests in Go? How do you write unit tests in Go? Mar 21, 2025 pm 06:34 PM

How do you write unit tests in Go?

See all articles