没有go.mod文件

原因是当前目录没有go.mod文件,因此vscode会出现如下报错

1
2
3
4
5
6
gopls was not able to find modules in your workspace.
When outside of GOPATH, gopls needs to know which modules
you are working on.You can fix this by opening your workspace
to a folder inside a Go module, or by using a go.work file
to specify multiple m

执行命令行($ go mod init packagestudy)即可
在文件同级目录下初始化一个新的Go模块,会自动创建一个(go.mod)文件