4

Firstly, I want to say thank u for reading my topic. I am newbie in Golang so nice to receive your help.
I am using golang for php-fpm-exporter (https://github.com/hipages/php-fpm_exporter)
My environment 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"

when I try 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)

In bash file (PHP-FPM.sh), I run this command

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

I don't know to fix this error :(
Thanks <3

TonyMoly312
  • 91
  • 1
  • 2
  • 6
  • Does [this](https://stackoverflow.com/questions/61845013/package-xxx-is-not-in-goroot-when-building-golang-project) help? – tink Jan 05 '21 at 03:54
  • @tink I have read it but I don't know what folder I will delete go mod ? Cause I dont use ```go mod init``` before :( – TonyMoly312 Jan 05 '21 at 03:58
  • **Never** use `go run main.go`. Get used to `go build` and then start the generated executable. A beginner has basically 0 chance to get `go run` with file arguments correct. – Volker Jan 05 '21 at 06:22

2 Answers2

3

This Error mostly occur when your project is not inside GoPATH to solve this issue use GO111MODULE=auto (Click here for detail)

In Go, The Project is supposed to be at a specific location(GOPATH) to solve this problem Go Modules come into the picture which helps us to run the go program even outside the go path. By Default, Go Language use GoPATH you can change it to GoModules by changing the environment variable GO11MODULE to either auto (will use GO Modules if your project is not inside GoPATH) or on (will always use GO Modules even if your project is at GOPATH)

NOTE: Looking at your project it looks like you have already initilized the Go Modules (presence of go.mod) That's why I didn't suggest initializing go modules (which can be done by running go mod init)

  • I modified GO111MODULE by ```export GO111MODULE=auto``` but it seems not changing :( – TonyMoly312 Jan 06 '21 at 01:51
  • Try running `go mod download` after `go mod init` and then see the result – Nupur Thakur Jan 06 '21 at 06:43
  • @PhuToan when you export any variable it only changes for that session. to permanently update the env variable you will need to set it's value in `$HOME/.profile ` file just like you would have done for `GOPATH` – Nupur Thakur Jan 06 '21 at 06:44
  • when I change the value of GO111MODULE, I checked ```go env``` it changed, but the result seems not changing. I am trying other way, I move my project into ```GOPATH``` and I turn off GO111MODULE, it return other result below – TonyMoly312 Jan 06 '21 at 06:51
  • ``` ../src/github.com/spf13/viper/viper.go:49:2: cannot find package "gopkg.in/ini.v1" in any of: /home/toandp99/local/go/src/gopkg.in/ini.v1 (from $GOROOT) /home/toandp99/go/src/gopkg.in/ini.v1 (from $GOPATH) ../src/github.com/spf13/viper/viper.go:50:2: cannot find package "gopkg.in/yaml.v2" in any of: /home/toandp99/local/go/src/gopkg.in/yaml.v2 (from $GOROOT) /home/toandp99/go/src/gopkg.in/yaml.v2 (from $GOPATH)``` – TonyMoly312 Jan 06 '21 at 06:52
  • I run command ```go mod init ``` in my project, right? :( – TonyMoly312 Jan 06 '21 at 06:58
  • @PhuToan Try to set `GOROOT` as `"/usr/local/go"` – Nupur Thakur Jul 19 '21 at 19:22
1

In my case I had the permission GOROOT issue in docker because the "go.mod" and "go.sum" files were not at the correct directory location.