php小編百草為你帶來解決「包foo不在GOROOT」的問題的方法。當我們在使用Go語言程式設計時,有時會遇到這樣的錯誤提示。這個錯誤通常意味著我們正在嘗試匯入一個包,但是該包不在Go語言的安裝目錄(GOROOT)中。在本文中,我們將為你詳細介紹如何解決這個問題,讓你的Go程式碼順利匯入所需的套件。
問題內容
我正在嘗試編譯我的 Go 項目,但修復編譯錯誤的方法並不明顯。簡化示例如下。
爸爸6a68db90e55e62259c35b03c780c3現在我使用make dep
(與Makefile
,如下)建立go.mod
並嘗試取得所有依賴項,但這並沒有取得它們,因為我一直看到package foo is not in GOROOT
錯誤。
<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>
這是我的~/.bashrc
,它導出了幾個相關的go環境變數。
<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>
這是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>
這是我在 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 protected]/ssh/transport.go:8:2: package bufio is not in GOROOT (/home/me/src/bufio) ../../gobin/pkg/mod/github.com/mattn/[email protected]/noncolorable.go:4:2: package bytes is not in GOROOT (/home/me/src/bytes)
因此,讓我們嘗試使用 go get
和 sudo 直接下載,以解決任何潛在的權限問題...
<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>
問題
我的 go
二進位檔案位於 ~/bin/go
中。
在我的 Makefile
中取得/指定依賴項(不列出無盡的子依賴項)並使該專案編譯的最有效方法是什麼?我想修復上面列出的所有編譯問題。
RTFM 不是答案。 RTFM 提供了有效文件的直接連結。
解決方法
-
問題標題(以及最初的問題正文)提到了
GOROOT
- 您不應將 GOROOT 設定為任何內容。
- 我根本不知道為什麼 Go 仍然在錯誤訊息中提到
GOROOT
。
-
簡短的故事,您的 Go 安裝似乎已損壞...修復它:
- 從 下載適合您的處理器架構的 Go 原始碼樹(對於典型的 Linux 安裝,通常是 amd64) go.dev/dl
cd /usr/local
- 將 tarball 提取為 root
cd ~/bin
#ln -s /usr/local/go/bin/go go
#ln -s /usr/local/go/bin/gofmt gofmt
- #將
cd
放入您的專案目錄中,並依照您設定Makefile
的方式進行建置。現在應該可以工作了。
以上是解決:包foo不在GOROOT中的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Go語言的核心特性包括垃圾回收、靜態鏈接和並發支持。 1.Go語言的並發模型通過goroutine和channel實現高效並發編程。 2.接口和多態性通過實現接口方法,使得不同類型可以統一處理。 3.基本用法展示了函數定義和調用的高效性。 4.高級用法中,切片提供了動態調整大小的強大功能。 5.常見錯誤如競態條件可以通過gotest-race檢測並解決。 6.性能優化通過sync.Pool重用對象,減少垃圾回收壓力。

Go語言在構建高效且可擴展的系統中表現出色,其優勢包括:1.高性能:編譯成機器碼,運行速度快;2.並發編程:通過goroutines和channels簡化多任務處理;3.簡潔性:語法簡潔,降低學習和維護成本;4.跨平台:支持跨平台編譯,方便部署。

關於SQL查詢結果排序的疑惑學習SQL的過程中,常常會遇到一些令人困惑的問題。最近,筆者在閱讀《MICK-SQL基礎�...

golang ...

Go語言中如何對比並處理三個結構體在Go語言編程中,有時需要對比兩個結構體的差異,並將這些差異應用到第�...

GoLand中自定義結構體標籤不顯示怎麼辦?在使用GoLand進行Go語言開發時,很多開發者會遇到自定義結構體標籤在�...


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

記事本++7.3.1
好用且免費的程式碼編輯器

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。