在目录中不存在 go.mod 文件

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

GO no go.mod file exists in directory

问题

我正在使用GO开始,并且想要创建一个类似Python中的虚拟环境(将导入的模块存储在项目目录本身),我在GO的文档https://go.dev/doc/tutorial/workspaces中阅读到,了解到GO的创建工作区是我需要做的。但是当我这样做时,它不起作用。就像附图中所示,在"go mod init .../hello_go"完成后,然后"go work init ./hello_go"出现了一些问题。

我不明白问题出在哪里?

在目录中不存在 go.mod 文件

英文:

I'm starting with GO, and I want to create a virtualenv like in Python (to store import modules at the project directory itself), I read in GO's doc https://go.dev/doc/tutorial/workspaces and understand that GO's Create the workspace is what i'm doing need. But when I do, it doesn't work. Like in the attached image, after "go mod init .../hello_go" complete, then "go work init ./hello_go" and something wrong.

I don't understand what is the problem?

在目录中不存在 go.mod 文件

答案1

得分: 0

问题在于go.work文件应该位于项目的根目录,并指向包含go.mod文件的子目录。

错误提示告诉你,没有./hello_go目录包含一个go.mod文件。这是正确的,因为你已经在根级别初始化了你的模块。

如果你只有一个模块,你不需要创建一个工作区。你可以像你所做的那样在根级别创建你的模块,然后使用go mod来管理该特定模块的依赖关系。

英文:

The problem is that the go.work file is supposed to be at the root of your project and point to subdirectories containing a go.mod file.

The error is telling you that there is no directory ./hello_go containing a go.mod file. This is correct because you have initialized your module also at the root level.

If you have only a single module, you don't need to create a workspace. You can create your module at root level like you did, and then use go mod to manage your dependencies for that particular module.

huangapple
  • 本文由 发表于 2022年5月29日 15:08:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/72421632.html
匿名

发表评论

匿名网友

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

确定