Home  >  Article  >  Backend Development  >  Solution: Package foo is not in GOROOT

Solution: Package foo is not in GOROOT

WBOY
WBOYforward
2024-02-09 10:48:07869browse

Solution: Package foo is not in GOROOT

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.

Question content

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&#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)

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>

question

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.

Solution

  • The question title (and the original question text) mentions GOROOT

  • Short story, your Go installation appears to be corrupted...fix it:

  1. Download the Go source tree appropriate for your processor architecture (usually amd64 for a typical Linux installation) from go.dev/dl
  2. cd /usr/local
  3. Extract tarball as root
  4. cd ~/bin
  5. ln -s /usr/local/go/bin/go go
  6. ln -s /usr/local/go/bin/gofmt gofmt
  7. Put 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!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete