Go error: go : go.mod file not found in current directory or any parent directory; (working on GOPATH/src)

Viewed 22104

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

4 Answers

"The go command now builds packages in module-aware mode by default, even when no go.mod is present."

"You can set GO111MODULE to auto to enable module-aware mode only when a go.mod file is present in the current directory or any parent directory."

At your command prompt

go env -w GO111MODULE=auto

New module changes in Go 1.16

Learning golang as well and I ran into this issue as well.

This solved it for me:

go mod init

That will create a base go.mod file with the module and version information to run go install if working out of the $GOPATH workspace. I do agree learning more about the Go module system and not turning off the env var, but to get up and running this seemed fine.

As of Go 1.16, the GO111MODULE environment variable is treated as "on" by default, meaning Go expects to find a go.mod file, and no longer falls back to pre-module GOPATH behavior.

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 a go.mod file.

See https://golang.org/doc/go1.16#go-command and https://golang.org/ref/mod

Create a go.mod file, examples:

go mod init example.com/m    # to initialize a v0 or v1 module
go mod init example.com/m/v2 # to initialize a v2 module
Related