Table of Contents
Question content
question
Solution
Home Backend Development Golang Solution: Package foo is not in GOROOT

Solution: Package foo is not in GOROOT

Feb 09, 2024 am 10:48 AM
go language Compile Error overflow

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>
Copy after login

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>
Copy after login

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>
Copy after login

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)
Copy after login

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>
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Which libraries in Go are developed by large companies or provided by well-known open source projects? Which libraries in Go are developed by large companies or provided by well-known open source projects? Apr 02, 2025 pm 04:12 PM

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

Why is it necessary to pass pointers when using Go and viper libraries? Why is it necessary to pass pointers when using Go and viper libraries? Apr 02, 2025 pm 04:00 PM

Go pointer syntax and addressing problems in the use of viper library When programming in Go language, it is crucial to understand the syntax and usage of pointers, especially in...

Why do all values ​​become the last element when using for range in Go language to traverse slices and store maps? Why do all values ​​become the last element when using for range in Go language to traverse slices and store maps? Apr 02, 2025 pm 04:09 PM

Why does map iteration in Go cause all values ​​to become the last element? In Go language, when faced with some interview questions, you often encounter maps...

How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? Apr 02, 2025 pm 04:54 PM

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

How to use Vue 3 to implement up scrolling loading function similar to WeChat chat records? How to use Vue 3 to implement up scrolling loading function similar to WeChat chat records? Apr 04, 2025 pm 03:51 PM

How to achieve upward scrolling loading similar to WeChat chat records? When developing applications similar to WeChat chat records, a common question is how to...

In Go programming, how to correctly manage the connection and release resources between Mysql and Redis? In Go programming, how to correctly manage the connection and release resources between Mysql and Redis? Apr 02, 2025 pm 05:03 PM

Resource management in Go programming: Mysql and Redis connect and release in learning how to correctly manage resources, especially with databases and caches...

What are the limitations of C user identifiers for different compilers? What are the limitations of C user identifiers for different compilers? Apr 03, 2025 pm 12:33 PM

A C identifier consists of letters, numbers and underscores, and the first character must be a letter or underscore. Different compilers have very different restrictions on identifiers: GCC: supports longer identifiers, and the character set limit is loose; Visual C: the identifier length is limited to 255 characters, and the support for special characters is limited; other compilers (such as embedded systems): the restrictions are stricter, and only support ASCII character sets are supported. When writing cross-platform code, be careful to follow the identifier naming specifications to avoid problems caused by compiler differences.

How to use the clip-path attribute of CSS to achieve the 45-degree curve effect of segmenter? How to use the clip-path attribute of CSS to achieve the 45-degree curve effect of segmenter? Apr 04, 2025 pm 11:45 PM

How to achieve the 45-degree curve effect of segmenter? In the process of implementing the segmenter, how to make the right border turn into a 45-degree curve when clicking the left button, and the point...

See all articles