I installed Golang and faced with go.mod file not found in current directory or any parent directory error at very first time.
But I'm working on <GOPATH>/src directory. Isn't go.mod only required if current working directory is located out of GOPATH?
Here's detailed information
Go version : go version go1.16.4 windows/amd64
Go env :
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\<userName>\AppData\Local\go-build
set GOENV=C:\Users\<userName>\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\<userName>\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\<userName>\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.16.4
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 -fmessage-length=0 -fdebug-prefix-map=C:\Users\<userName>\AppData\Local\Temp\go-build4258913208=/tmp/go-build -gno-record-gcc-switches
Working directory: C:\Users\<userName>\go\src\main.go
Code :
package main
import "fmt"
func main() {
fmt.Println("HELLO")
}
And Error:
go: go.mod file not found in current directory or any parent directory; see 'go help modules'
Build process exiting with code: 1 signal: null
p.s. I'm using VS Code
As of Go 1.16, the
GO111MODULEenvironment variable is treated as "on" by default, meaning Go expects to find ago.modfile, and no longer falls back to pre-moduleGOPATHbehavior.If you want to go back to the pre-1.16 behavior, you now have to explicitly specify
GO111MODULE=auto, but you're far better off creating ago.modfile.See https://golang.org/doc/go1.16#go-command and https://golang.org/ref/mod