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
放入您的项目目录中,并按照您设置cd
放入您的项目目录中,并按照您设置Makefile
的方式进行构建。现在应该可以工作了。
以上是解决:包foo不在GOROOT中的详细内容。更多信息请关注PHP中文网其他相关文章!

掌握Go语言中的strings包可以提高文本处理能力和开发效率。1)使用Contains函数检查子字符串,2)用Index函数查找子字符串位置,3)Join函数高效拼接字符串切片,4)Replace函数替换子字符串。注意避免常见错误,如未检查空字符串和大字符串操作性能问题。

你应该关心Go语言中的strings包,因为它能简化字符串操作,使代码更清晰高效。1)使用strings.Join高效拼接字符串;2)用strings.Fields按空白符分割字符串;3)通过strings.Index和strings.LastIndex查找子串位置;4)用strings.ReplaceAll进行字符串替换;5)利用strings.Builder进行高效字符串拼接;6)始终验证输入以避免意外结果。

thestringspackageingoisesential forefficientstringManipulation.1)itoffersSimpleyetpoperfulfunctionsFortaskSlikeCheckingSslingSubstringsStringStringsStringsandStringsN.2)ithandhishiCodeDewell,withFunctionsLikestrings.fieldsfieldsfieldsfordsforeflikester.fieldsfordsforwhitespace-fieldsforwhitespace-separatedvalues.3)3)

WhendecidingbetweenGo'sbytespackageandstringspackage,usebytes.Bufferforbinarydataandstrings.Builderforstringoperations.1)Usebytes.Bufferforworkingwithbyteslices,binarydata,appendingdifferentdatatypes,andwritingtoio.Writer.2)Usestrings.Builderforstrin

Go的strings包提供了多种字符串操作功能。1)使用strings.Contains检查子字符串。2)用strings.Split将字符串分割成子字符串切片。3)通过strings.Join合并字符串。4)用strings.TrimSpace或strings.Trim去除字符串首尾的空白或指定字符。5)用strings.ReplaceAll替换所有指定子字符串。6)使用strings.HasPrefix或strings.HasSuffix检查字符串的前缀或后缀。

使用Go语言的strings包可以提升代码质量。1)使用strings.Join()优雅地连接字符串数组,避免性能开销。2)结合strings.Split()和strings.Contains()处理文本,注意大小写敏感问题。3)避免滥用strings.Replace(),考虑使用正则表达式进行大量替换。4)使用strings.Builder提高频繁拼接字符串的性能。

Go的bytes包提供了多种实用的函数来处理字节切片。1.bytes.Contains用于检查字节切片是否包含特定序列。2.bytes.Split用于将字节切片分割成smallerpieces。3.bytes.Join用于将多个字节切片连接成一个。4.bytes.TrimSpace用于去除字节切片的前后空白。5.bytes.Equal用于比较两个字节切片是否相等。6.bytes.Index用于查找子切片在largerslice中的起始索引。

theEncoding/binarypackageingoisesenebecapeitProvidesAstandArdArdArdArdArdArdArdArdAndWriteBinaryData,确保Cross-cross-platformCompatibilitiational and handhandlingdifferentendenness.itoffersfunctionslikeread,写下,写,dearte,readuvarint,andwriteuvarint,andWriteuvarIntforPreciseControloverBinary


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Atom编辑器mac版下载
最流行的的开源编辑器

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

禅工作室 13.0.1
功能强大的PHP集成开发环境

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

Dreamweaver Mac版
视觉化网页开发工具