vscode 错误 go: go.mod file not found in current directory or any parent directory; see 'go help modules'

发布时间 2023-12-21 14:44:25作者: xueweil

前言

安装VSCODE 后,新建立的GO文件按F5出错。go: go.mod file not found in current directory or any parent directory; see 'go help modules'

处理步骤

开启go modules功能

命令行输入 go env -w GO111MODULE=on

建立src目录

比如我程序文件夹名为 gosrc,则目录为
gosrc
|_src 放源文件

初始化Go moudle

在项目根目录下,初始化Go moudle
命令行输入 go mod init gosrc
输入完成后,根目录下会多一个gosrc.mod的文件

建立GO文件

建立一个测试文件 如test.go,输入完代码后按F5运行,程序应该就可正确输出“hello world” 了.

`golang

package main
func main() {
print("hello world")
}

`