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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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)

What are the vulnerabilities of Debian OpenSSL What are the vulnerabilities of Debian OpenSSL Apr 02, 2025 am 07:30 AM

OpenSSL, as an open source library widely used in secure communications, provides encryption algorithms, keys and certificate management functions. However, there are some known security vulnerabilities in its historical version, some of which are extremely harmful. This article will focus on common vulnerabilities and response measures for OpenSSL in Debian systems. DebianOpenSSL known vulnerabilities: OpenSSL has experienced several serious vulnerabilities, such as: Heart Bleeding Vulnerability (CVE-2014-0160): This vulnerability affects OpenSSL 1.0.1 to 1.0.1f and 1.0.2 to 1.0.2 beta versions. An attacker can use this vulnerability to unauthorized read sensitive information on the server, including encryption keys, etc.

How do you use the pprof tool to analyze Go performance? How do you use the pprof tool to analyze Go performance? Mar 21, 2025 pm 06:37 PM

The article explains how to use the pprof tool for analyzing Go performance, including enabling profiling, collecting data, and identifying common bottlenecks like CPU and memory issues.Character count: 159

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

The article discusses writing unit tests in Go, covering best practices, mocking techniques, and tools for efficient test management.

What libraries are used for floating point number operations in Go? What libraries are used for floating point number operations in Go? Apr 02, 2025 pm 02:06 PM

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

What is the problem with Queue thread in Go's crawler Colly? What is the problem with Queue thread in Go's crawler Colly? Apr 02, 2025 pm 02:09 PM

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

Transforming from front-end to back-end development, is it more promising to learn Java or Golang? Transforming from front-end to back-end development, is it more promising to learn Java or Golang? Apr 02, 2025 am 09:12 AM

Backend learning path: The exploration journey from front-end to back-end As a back-end beginner who transforms from front-end development, you already have the foundation of nodejs,...

How to specify the database associated with the model in Beego ORM? How to specify the database associated with the model in Beego ORM? Apr 02, 2025 pm 03:54 PM

Under the BeegoORM framework, how to specify the database associated with the model? Many Beego projects require multiple databases to be operated simultaneously. When using Beego...

How do you specify dependencies in your go.mod file? How do you specify dependencies in your go.mod file? Mar 27, 2025 pm 07:14 PM

The article discusses managing Go module dependencies via go.mod, covering specification, updates, and conflict resolution. It emphasizes best practices like semantic versioning and regular updates.

See all articles