Heim  >  Artikel  >  Backend-Entwicklung  >  go grpc: github.com/golang/protobuf/proto kann nicht importiert werden (kein erforderliches Modul stellt Paket „github.com/golang/protobuf/proto“ bereit)

go grpc: github.com/golang/protobuf/proto kann nicht importiert werden (kein erforderliches Modul stellt Paket „github.com/golang/protobuf/proto“ bereit)

王林
王林nach vorne
2024-02-09 09:00:111139Durchsuche

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

PHP-Editor Zimo ist bei der Verwendung von go grpc auf einen Fehler gestoßen, der darauf hinweist, dass das Modul „github.com/golang/protobuf/proto“ nicht importiert werden konnte. Dieser Fehler wird normalerweise durch fehlende erforderliche Module verursacht. Bevor wir go grpc verwenden, müssen wir sicherstellen, dass die Protobuf-Bibliothek korrekt installiert und die relevanten Proto-Pakete korrekt in den Code importiert wurden. Als nächstes werde ich im Detail vorstellen, wie dieses Problem gelöst werden kann.

Frageninhalt

Wenn „protoc --proto_path=proto proto/*.proto --go_out=plugins“ lautet, importiert die Proto-Datei „github.com/golang/protobuf/proto“ anstelle von „google.golang.org „/protobuf/proto“ =grpc:pb“-Befehl

Dateien importieren

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

Meine Prototypendatei

syntax="proto3";

message Processor{

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

~go/bin/protoc-gen-go-grpc hat eine 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

Was habe ich getan

Erstinstallation

$ installieren Sie google.golang.org/protobuf/cmd/[email protected]

$ installieren Sie google.golang.org/grpc/cmd/[email protected]

Geben Sie go clean -modcache ein, bevor Sie neue Pakete installieren, und installieren Sie die neueste Version mit dem Kommentar @latest

neu

go-Version: go-Version go1.21.1 linux/amd64

auf Ubuntu 20.4

Protokoll--Version libprotoc 3.6.1

Protobuf-Compiler und Golang-Goprotobuf mit apt installiert

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

Ich denke, das Problem liegt hier, aber ich weiß nicht, was ich beheben soll oder wie ich das lesen soll

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)

BEARBEITEN: Ich glaube, ich habe es ursprünglich mit go get -u github.com/golang/protobuf/proto installiert, aber ich habe die Binärdatei mit rm -rf $(go env GOPATH)/pkg/mod/github.com/golang file/ entfernt. protobuf/proto und installieren Sie die neue Version mit go install google.golang.org/protobuf/cmd/protoc-gen-go@latest und go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@ Latest . Es generiert immer noch Go-Dateien mit den alten Importen

Edit2: protoc-gen-go --version nicht gefunden, aber protoc-gen-go-grpc --version ist 1.2.0. protoc – Die Version ist libprotoc 3.6.1 Wo ist 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 p>

Die Lösung

ist wie folgt: https://www.php.cn/link/a5481cd6d7517aa3fc6476dc7d9019ab Autor: @puellanivis

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

Bearbeiten ~/.bashrc~/.bash_profile 文件 ($vim ~/.bashrc) und exportieren Sie die gesamte Pfadumgebung manuell. In meinem Fall muss ich

hinzufügen
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

Das obige ist der detaillierte Inhalt vongo grpc: github.com/golang/protobuf/proto kann nicht importiert werden (kein erforderliches Modul stellt Paket „github.com/golang/protobuf/proto“ bereit). Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Dieser Artikel ist reproduziert unter:stackoverflow.com. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen