登录
首页 >  Golang >  Go问答

当“go run”时出现错误“Package ... is not in GOROOT”

来源:stackoverflow

时间:2024-04-06 12:45:34 240浏览 收藏

学习知识要善于思考,思考,再思考!今天golang学习网小编就给大家带来《当“go run”时出现错误“Package ... is not in GOROOT”》,以下内容主要包含等知识点,如果你正在学习或准备学习Golang,就都不要错过本文啦~让我们一起来看看吧,能帮助到你就更好了!

问题内容

首先,我想说感谢您阅读我的主题。我是 golang 的新手,很高兴得到你的帮助。

我正在使用 golang 进行 php-fpm-exporter (https://github.com/hipages/php-fpm_exporter)

我的环境go env

go111module=""
goarch="amd64"
gobin=""
gocache="/home/toandp99/.cache/go-build"
goenv="/home/toandp99/.config/go/env"
goexe=""
goflags=""
gohostarch="amd64"
gohostos="linux"
goinsecure=""
gonoproxy=""
gonosumdb=""
goos="linux"
gopath="/home/toandp99/go"
goprivate=""
goproxy="https://proxy.golang.org,direct"
goroot="/home/toandp99/local/go"
gosumdb="sum.golang.org"
gotmpdir=""
gotooldir="/home/toandp99/local/go/pkg/tool/linux_amd64"
gccgo="gccgo"
ar="ar"
cc="gcc"
cxx="g++"
cgo_enabled="1"
gomod=""
cgo_cflags="-g -o2"
cgo_cppflags=""
cgo_cxxflags="-g -o2"
cgo_fflags="-g -o2"
cgo_ldflags="-g -o2"
pkg_config="pkg-config"
gogccflags="-fpic -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build100112555=/tmp/go-build -gno-record-gcc-switches"

当我尝试 php-fpm-exporter

toandp99@toandp99-x555uj:~/php-fpm_exporter$ ls
cmd                 dockerfile  license   phpfpm       readme.md
code_of_conduct.md  go.mod      main.go   php-fpm.pid  sonar-project.properties
config.sh           go.sum      makefile  php-fpm.sh   test
toandp99@toandp99-x555uj:~/php-fpm_exporter$ ./php-fpm.sh start
starting monitoring fastcgi process manager...go: downloading github.com/spf13/cobra v0.0.7
go: downloading github.com/speps/go-hashids v2.0.0+incompatible
go: downloading github.com/davecgh/go-spew v1.1.1
go: downloading github.com/gosuri/uitable v0.0.4
go: downloading github.com/tomasen/fcgi_client v0.0.0-20180423082037-2bb3d819fd19
go: downloading github.com/fatih/color v1.9.0
go: downloading github.com/mattn/go-isatty v0.0.12
go: downloading golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527
go: downloading github.com/spf13/pflag v1.0.5
go: downloading github.com/mattn/go-colorable v0.1.6
go: downloading github.com/prometheus/client_golang v1.5.1
go: downloading github.com/mattn/go-runewidth v0.0.8
go: downloading github.com/prometheus/common v0.9.1
go: downloading github.com/prometheus/client_model v0.2.0
go: downloading github.com/cespare/xxhash v1.1.0
go: downloading github.com/prometheus/procfs v0.0.10
go: downloading github.com/golang/protobuf v1.3.4
go: downloading github.com/cespare/xxhash/v2 v2.1.1
go: downloading github.com/beorn7/perks v1.0.1
go: downloading github.com/matttproud/golang_protobuf_extensions v1.0.1
cmd/server.go:29:2: package client_golang-master/prometheus is not in goroot (/home/toandp99/local/go/src/client_golang-master/prometheus)
cmd/server.go:30:2: package client_golang-master/prometheus/promhttp is not in goroot (/home/toandp99/local/go/src/client_golang-master/prometheus/promhttp)
cmd/root.go:29:2: package cobra-master is not in goroot (/home/toandp99/local/go/src/cobra-master)
cmd/root.go:27:2: package go-homedir is not in goroot (/home/toandp99/local/go/src/go-homedir)
cmd/root.go:28:2: package logrus-master is not in goroot (/home/toandp99/local/go/src/logrus-master)
cmd/root.go:26:2: package php-fpm_exporter/phpfpm is not in goroot (/home/toandp99/local/go/src/php-fpm_exporter/phpfpm)
cmd/root.go:30:2: package viper-master is not in goroot (/home/toandp99/local/go/src/viper-master)

在 bash 文件(php-fpm.sh)中,我运行此命令

go run main.go server --web.listen-address $PHP_FPM_WEB_LISTEN_ADDRESS --log.level=$PHP_FPM_LOG_LEVEL --phpfpm.scrape-uri $PHP_FPM_SCRAPE_URI --web.telemetry-path $PHP_FPM_WEB_TELEMETRY_PATH --phpfpm.fix-process-count = $PHP_FPM_FIX_PROCESS_COUNT

我不知道如何修复这个错误:(

感谢<3


解决方案


此错误大多发生在您的项目不在 GoPATH 内时,使用 GO111MODULE=auto 解决此问题(详细信息为 Click here

在 Go 中,项目应该位于特定位置(GOPATH)来解决这个问题。Go 模块的出现可以帮助我们甚至在 go 路径之外运行 go 程序。默认情况下,Go 语言使用 GoPATH,您可以通过将环境变量 GO11MODULE 更改为 auto (如果您的项目不在 GoPATH 内,则将使用 GO 模块)或 on (即使您的项目位于 GOPATH 中,也将始终使用 GO 模块)来将其更改为 GoModules )

注意: 查看您的项目,看起来您已经初始化了 Go 模块(存在 go.mod),这就是为什么我不建议初始化 go 模块(可以通过运行 go mod init 来完成)

In我的情况是,我在 docker 中遇到了 GOROOT 权限问题,因为“go.mod”和“go.sum”文件不在正确的目录位置。

终于介绍完啦!小伙伴们,这篇关于《当“go run”时出现错误“Package ... is not in GOROOT”》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>