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

huangapple go评论86阅读模式
英文:

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

问题

我安装了Golang,并在第一次使用时遇到了go.mod文件在当前目录或任何父目录中找不到的错误。

但是我正在<GOPATH>/src目录中工作。难道只有当当前工作目录位于GOPATH之外时,才需要go.mod吗?

以下是详细信息

Go版本:go version go1.16.4 windows/amd64

Go环境:

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

工作目录:C:\Users\<userName>\go\src\main.go

代码:

package main

import "fmt"

func main() {
	fmt.Println("HELLO")
}

错误信息:

go: go.mod文件在当前目录或任何父目录中找不到;请参阅'go help modules'
构建过程以代码1信号null退出

附注:我正在使用VS Code

英文:

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 &lt;GOPATH&gt;/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\&lt;userName&gt;\AppData\Local\go-build
set GOENV=C:\Users\&lt;userName&gt;\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\&lt;userName&gt;\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\&lt;userName&gt;\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\&lt;userName&gt;\AppData\Local\Temp\go-build4258913208=/tmp/go-build -gno-record-gcc-switches

Working directory: C:\Users\&lt;userName&gt;\go\src\main.go

Code :

package main

import &quot;fmt&quot;

func main() {
	fmt.Println(&quot;HELLO&quot;)
}

And Error:

go: go.mod file not found in current directory or any parent directory; see &#39;go help modules&#39;
Build process exiting with code: 1 signal: null

p.s. I'm using VS Code

答案1

得分: 15

"go命令现在默认以模块感知模式构建包,即使没有go.mod文件存在。

您可以将GO111MODULE设置为auto,仅当当前目录或任何父目录中存在go.mod文件时才启用模块感知模式。

在命令提示符下执行以下命令:

go env -w GO111MODULE=auto

Go 1.16中的新模块更改

英文:

"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

答案2

得分: 7

学习golang时,我也遇到了这个问题。

这个方法解决了我的问题:

go mod init

这将创建一个基本的go.mod文件,其中包含模块和版本信息,以便在$GOPATH工作空间中运行go install。我同意学习更多关于Go模块系统的知识,而不是关闭环境变量,但为了快速上手,这个方法似乎是可以的。

英文:

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.

答案3

得分: 5

从Go 1.16版本开始,默认情况下将GO111MODULE环境变量视为“on”,这意味着Go希望找到一个go.mod文件,不再回退到之前的模块GOPATH行为。

如果你想返回到1.16之前的行为,现在必须显式地指定GO111MODULE=auto,但最好还是创建一个go.mod文件。

请参阅https://golang.org/doc/go1.16#go-command和https://golang.org/ref/mod。

英文:

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

答案4

得分: 0

创建一个go.mod文件,示例如下:

go mod init example.com/m    # 初始化一个v0或v1模块
go mod init example.com/m/v2 # 初始化一个v2模块
英文:

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

huangapple
  • 本文由 发表于 2021年6月11日 08:21:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/67929883.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定