Error package xxx is not in GOROOT or GOPATH or cannot find package “xxx” in any of

Problem Description:

~/Documents/goproject/src/testproject01/main/unit5/demo09/crm/main » go run main.go                     kongfanyu@192
main.go:6:8: package testproject01/main/unit5/demo09/crm/dbutils is not in GOROOT (/usr/local/go/src/testproject01/main/unit5/demo09/crm/dbutils)

Project structure:

Solution:

1. View the path of GOPATH

~ » go env                                     kongfanyu@kongfanyudeMacBook-
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/kongfanyu/Library/Caches/go-build"
GOENV="/Users/kongfanyu/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Documents/goproject/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Documents/goproject"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.17.6"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/lz/ktdhrh1s7w3ggytjy5046dxh0000gn/T/go-build3400769310=/tmp/go-build -gno-record-gcc-switches -fno-common"
------------------------------------------------------------
~ » pwd            

Note that the path of GOPATH is wrong, not pointing to the current project;

2. Modify the GOPATH path

~/Documents » go env -w GO111MODULE=off   
~/Documents » go env -w GOPATH=/Users/kongfanyu/Documents/goproject/ 

Be careful not to bring the src path, otherwise there will be an extra src folder.

3. Execute again

~/Documents/goproject/src/testproject01/main/unit5/demo09/crm/main » go run main.go                             
您好,这是main函数的执行......
新年好,这是utils包getConn函数的执行......

Supongo que te gusta

Origin blog.csdn.net/kongfanyu/article/details/122753248
Recomendado
Clasificación