报错package xxx is not in GOROOT or GOPATH 或者 cannot find package “xxx“ in any of

问题描述:

~/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)

项目结构:

报错package xxx is not in GOROOT or GOPATH 或者 cannot find package “xxx“ in any of_第1张图片

解决办法:

1. 查看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            

注意GOPATH的路径不对,不是指向当前项目的;

2. 修改GOPATH路径

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

注意不用带src路径,否则会多一个src文件夹。

3. 再次执行即可

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

你可能感兴趣的:(Go语言,golang,开发语言,后端)