php editor Baicao brings you a solution to the problem of "package foo is not in GOROOT". When we use Go language programming, we sometimes encounter such error prompts. This error usually means that we are trying to import a package, but the package is not in the Go language installation directory (GOROOT). In this article, we will introduce you in detail how to solve this problem so that your Go code can smoothly import the required packages.
I'm trying to compile my Go project, but the way to fix the compilation error is not obvious. A simplified example follows.
Dad6a68db90e55e62259c35b03c780c3 Now I build go.mod
using make dep
(with Makefile
, below) and try to get all the dependencies, but this doesn't get them, Because I keep seeing package foo is not in GOROOT
errors.
<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>
This is my ~/.bashrc
, which exports several related go environment variables.
<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>
This is go.mod
, generated by my 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>
This is what I saw when 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)
So let's try downloading directly using go get
and sudo to resolve any potential permission issues...
<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>
My go
binaries are located in ~/bin/go
.
What is the most efficient way to get/specify dependencies in my Makefile
(without listing endless sub-dependencies) and make this project compile? I want to fix all compilation issues listed above.
RTFM is not the answer. RTFM provides direct links to valid documents.
The question title (and the original question text) mentions GOROOT
GOROOT
in error messages. Short story, your Go installation appears to be corrupted...fix it:
cd /usr/local
cd ~/bin
ln -s /usr/local/go/bin/go go
ln -s /usr/local/go/bin/gofmt gofmt
cd
into your project directory and build it the way you set up your Makefile
. It should work now. The above is the detailed content of Solution: Package foo is not in GOROOT. For more information, please follow other related articles on the PHP Chinese website!