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 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>
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
- You should not set GOROOT to anything.
- I have no idea why Go still mentions
GOROOT
in error messages.
-
Short story, your Go installation appears to be corrupted...fix it:
- Download the Go source tree appropriate for your processor architecture (usually amd64 for a typical Linux installation) from go.dev/dl
cd /usr/local
- Extract tarball as root
cd ~/bin
ln -s /usr/local/go/bin/go go
ln -s /usr/local/go/bin/gofmt gofmt
- Put
cd
into your project directory and build it the way you set up yourMakefile
. 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!

go语言有缩进。在go语言中,缩进直接使用gofmt工具格式化即可(gofmt使用tab进行缩进);gofmt工具会以标准样式的缩进和垂直对齐方式对源代码进行格式化,甚至必要情况下注释也会重新格式化。

go语言叫go的原因:想表达这门语言的运行速度、开发速度、学习速度(develop)都像gopher一样快。gopher是一种生活在加拿大的小动物,go的吉祥物就是这个小动物,它的中文名叫做囊地鼠,它们最大的特点就是挖洞速度特别快,当然可能不止是挖洞啦。

是,TiDB采用go语言编写。TiDB是一个分布式NewSQL数据库;它支持水平弹性扩展、ACID事务、标准SQL、MySQL语法和MySQL协议,具有数据强一致的高可用特性。TiDB架构中的PD储存了集群的元信息,如key在哪个TiKV节点;PD还负责集群的负载均衡以及数据分片等。PD通过内嵌etcd来支持数据分布和容错;PD采用go语言编写。

go语言能编译。Go语言是编译型的静态语言,是一门需要编译才能运行的编程语言。对Go语言程序进行编译的命令有两种:1、“go build”命令,可以将Go语言程序代码编译成二进制的可执行文件,但该二进制文件需要手动运行;2、“go run”命令,会在编译后直接运行Go语言程序,编译过程中会产生一个临时文件,但不会生成可执行文件。

go语言需要编译。Go语言是编译型的静态语言,是一门需要编译才能运行的编程语言,也就说Go语言程序在运行之前需要通过编译器生成二进制机器码(二进制的可执行文件),随后二进制文件才能在目标机器上运行。

删除map元素的两种方法:1、使用delete()函数从map中删除指定键值对,语法“delete(map, 键名)”;2、重新创建一个新的map对象,可以清空map中的所有元素,语法“var mapname map[keytype]valuetype”。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
