Search code examples
gogo-modulesgo-work

GO no go.mod file exists in directory


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?

enter image description here


Solution

  • 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.