Heim  >  Artikel  >  Backend-Entwicklung  >  Lösung: Paket foo ist nicht in GOROOT

Lösung: Paket foo ist nicht in GOROOT

WBOY
WBOYnach vorne
2024-02-09 10:48:07870Durchsuche

Lösung: Paket foo ist nicht in GOROOT

php-Editor Baicao bietet Ihnen eine Lösung für das Problem „Paket foo ist nicht in GOROOT“. Wenn wir die Go-Sprachprogrammierung verwenden, stoßen wir manchmal auf solche Fehlermeldungen. Dieser Fehler bedeutet normalerweise, dass wir versuchen, ein Paket zu importieren, sich das Paket jedoch nicht im Installationsverzeichnis der Go-Sprache (GOROOT) befindet. In diesem Artikel stellen wir Ihnen detailliert vor, wie Sie dieses Problem lösen können, damit Ihr Go-Code die erforderlichen Pakete reibungslos importieren kann.

Frageninhalt

Ich versuche, mein Go-Projekt zu kompilieren, aber die Möglichkeit, den Kompilierungsfehler zu beheben, ist nicht offensichtlich. Es folgt ein vereinfachtes Beispiel.

Dad6a68db90e55e62259c35b03c780c3

Jetzt verwende ich make dep (与 Makefile,如下)构建 go.mod 并尝试获取所有依赖项,但这并没有获取它们,因为我一直看到 package foo is not in GOROOT falsch.

<code># filename: ~/myprojects/automate_things/Makefile
GOOS=linux

GO_SOURCE_FILE=automate_things.go
GO_BINARY_FILE=automate_things
GO_BIN_DIR=bin/

.SILENT: build
.DEFAULT_GOAL := build

build:
        -make fmt
        make dep
        go build $(GO_SOURCE_FILE)
        if [ "$(PLATFORM)" = "Linux" ]; then \
                GOARCH=amd64 GOOS=linux go build -ldflags "-s -w" -o ./$(GO_BINARY_FILE) $(GO_SOURCE_FILE); \
        elif [ "$(PLATFORM)" = "Darwin" ]; then \
                GOARCH=amd64 GOOS=darwin go build -ldflags "-s -w" -o ./$(GO_BINARY_FILE) $(GO_SOURCE_FILE); \
        fi
.PHONY: build

fmt:
        go fmt $(GO_SOURCE_FILE)
.PHONY: fmt

dep:
        -rm go.mod
        -rm go.sum
        go mod init automate_things
        go mod tidy
        go mod download
.PHONY: dep
</code>

Das ist meins~/.bashrc, es exportiert mehrere relevante Go-Umgebungsvariablen.

<code># filename: ~/.bashrc
# Build the default GOROOT (re-run the command as root)

mkdir -p /usr/local/go
export GOPROXY=https://proxy.golang.org
mkdir -p ~/gobin/bin
export GOPATH=~/gobin
#export GO111MODULE=auto

export PATH=$PATH:$GOPATH/bin
</code>

Dies wird generiert von go.mod,是我的Makefile(make dep)

<code>// go.mod
module automate_things

go 1.20

require (
        github.com/gleich/logoru v0.0.0-20230101033757-d86cd895c7a1
        github.com/melbahja/goph v1.4.0
)

require (
        github.com/fatih/color v1.10.0 // indirect
        github.com/kr/fs v0.1.0 // indirect
        github.com/mattn/go-colorable v0.1.8 // indirect
        github.com/mattn/go-isatty v0.0.12 // indirect
        github.com/pkg/errors v0.9.1 // indirect
        github.com/pkg/sftp v1.13.5 // indirect
        golang.org/x/crypto v0.6.0 // indirect
        golang.org/x/sys v0.5.0 // indirect
)
</code>

Das habe ich gesehen, als make dep

$ make
make[1]: Entering directory '/home/me/myprojects/automate_things'
go fmt automate_things.go
make[1]: Leaving directory '/home/me/myprojects/automate_things'
make[1]: Entering directory '/home/me/myprojects/automate_things'
rm go.mod
rm go.sum
go mod init automate_things
go: creating new go.mod: module automate_things
go: to add module requirements and sums:
        go mod tidy
go mod tidy
go: finding module for package github.com/melbahja/goph
go: finding module for package github.com/gleich/logoru
go: found github.com/gleich/logoru in github.com/gleich/logoru v0.0.0-20230101033757-d86cd895c7a1
go: found github.com/melbahja/goph in github.com/melbahja/goph v1.4.0
go mod download
make[1]: Leaving directory '/home/me/myprojects/automate_things'
../../gobin/pkg/mod/golang.org/x/[email&#160;protected]/ssh/transport.go:8:2: package bufio is not in GOROOT (/home/me/src/bufio)
../../gobin/pkg/mod/github.com/mattn/[email&#160;protected]/noncolorable.go:4:2: package bytes is not in GOROOT (/home/me/src/bytes)

Also versuchen wir es direkt mit go get und sudo herunterzuladen, um mögliche Berechtigungsprobleme zu lösen...

<code>$ sudo /home/me/bin/go get github.com/melbahja/goph
go: downloading github.com/melbahja/goph v1.4.0
go: downloading github.com/pkg/errors v0.9.1
go: downloading github.com/pkg/sftp v1.13.5
go: downloading golang.org/x/crypto v0.6.0
go: downloading github.com/kr/fs v0.1.0
go: downloading golang.org/x/sys v0.5.0
github.com/melbahja/goph imports
        context: package context is not in GOROOT (/usr/local/go/src/context)
github.com/melbahja/goph imports
        errors: package errors is not in GOROOT (/usr/local/go/src/errors)
github.com/melbahja/goph imports
</code>

Frage

Mein go 二进制文件位于 ~/bin/go ist da.

Was ist der effizienteste Weg, Abhängigkeiten in meinem Makefile abzurufen/anzugeben (ohne endlose Unterabhängigkeiten aufzulisten) und dieses Projekt kompilieren zu lassen? Ich möchte alle oben aufgeführten Kompilierungsprobleme beheben.

RTFM ist nicht die Antwort. RTFM bietet direkte Links zu gültigenDokumenten.

Lösung

  • Der Fragetitel (und der ursprüngliche Fragetext) erwähnt GOROOT

  • Kurz gesagt, Ihre Go-Installation scheint kaputt zu sein... beheben Sie das Problem:

  1. Laden Sie den für Ihre Prozessorarchitektur geeigneten Go-Quellbaum (normalerweise amd64 für eine typische Linux-Installation) von go.dev/dl
  2. herunter
  3. cd /usr/local
  4. Tarball als Root extrahieren
  5. cd ~/bin
  6. ln -s /usr/local/go/bin/go go
  7. ln -s /usr/local/go/bin/gofmt gofmt
  8. Legen Sie cd in Ihr Projektverzeichnis und erstellen Sie es so, wie Sie es eingerichtet haben cd 放入您的项目目录中,并按照您设置 Makefile. Es sollte jetzt funktionieren.

Das obige ist der detaillierte Inhalt vonLösung: Paket foo ist nicht in GOROOT. 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