go 不能导入自定义包 GO111MODULE

提示1:
# example1/cli
cli\main.go:20:10: cannot refer to unexported name c2.w

提示2:
PS E:\go\go-example1> go run ./cli
cli\main.go:6:5: local import "./../c2" in non-local package

提示3:
main.go:5:2: cannot find package "hello/mylib" in any of:
C:\Program Files\Go\src\hello\mylib (from $GOROOT)
C:\Users\Administrator\go\src\hello\mylib (from $GOPATH)

---------------------------

查询:
go env

C:\Users\administratorsXu>go env
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\administratorsXu\AppData\Local\go-build
set GOENV=C:\Users\administratorsXu\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=E:\studyCode\code\GO\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=E:\studyCode\code\GO
set GOPRIVATE=
set GOPROXY=https://goproxy.cn,direct
set GOROOT=E:\progranfiles\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=E:\progranfiles\go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.17.3
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments ...

GO111MODULE

用环境变量 GO111MODULE 开启或关闭模块支持,它有三个可选值:off、on、auto,默认值是 auto。

GO111MODULE=off 无模块支持,go 会从 GOPATH 和 vendor 文件夹寻找包。
GO111MODULE=on 模块支持,go 会忽略 GOPATH 和 vendor 文件夹,只根据 go.mod 下载依赖。
GO111MODULE=auto 在 $GOPATH/src 外面且根目录有 go.mod 文件时,开启模块支持。

解决方案
go env -w GO111MODULE=off

go env -w GO111MODULE=auto

 

此条目发表在go分类目录。将固定链接加入收藏夹。