英文:
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 <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
答案1
得分: 15
"go命令现在默认以模块感知模式构建包,即使没有go.mod文件存在。
您可以将GO111MODULE设置为auto,仅当当前目录或任何父目录中存在go.mod文件时才启用模块感知模式。
在命令提示符下执行以下命令:
go env -w GO111MODULE=auto
英文:
"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
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论