0

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

blackgreen
  • 34,072
  • 23
  • 111
  • 129
Hadilen
  • 21
  • 3

1 Answers1

0

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.

The Fool
  • 16,715
  • 5
  • 52
  • 86
  • ok thanks, I understand the problem, but I want to ask more, is using Create the workspace the same as helping me create a virtual environment for me to import modules from outside? Are my issues correct? – Hadilen May 29 '22 at 07:28
  • @Hadilen, go mod is sufficient for your needs. It's not a virtual env, but it helps you to manage your dependencies on a module basis. Each module has their own set of dependencies with their own version. You can also use go mod vendor, if you really want to save them in the same directory, but then you should also source control them, unlike virtual env. – The Fool May 29 '22 at 07:31
  • go doesnt need virtual envs like python does. When you use go mod, the dependencies are stored with different versions in `GOMODCACHE`. You can do `go env GOMODCACHE` to see where this might be. – The Fool May 29 '22 at 07:34