22

The question is in the title: Is there a GO equivalent to python's virtualenv? What is the prefered work flow to start a new project?

will.mendil
  • 752
  • 2
  • 6
  • 21
  • Not an expert on Go by any means but found this after some googling on the matter, see if it solves your issue: [Virtualgo](https://github.com/GetStream/vg) – Kastakin May 15 '20 at 07:03
  • 3
    [How to write Go code](https://golang.org/doc/code.html) describes the prefered work flow for a new project. – Charlie Tumahai May 15 '20 at 07:06
  • 3
    No, there is no "govirtualenv" for Go as there is no need. Go modules work fine and provide reproducible builds. – Volker May 15 '20 at 07:28
  • 2
    I don't know why this is being down voted. It might be an obvious answer to some, but with all the libraries out there, I was just looking for some direction. It is a boon question, but isn't this a forum to learn? Furthermore, the I looked for a similar question on the forum, and couldn't find so it was clearly lacking. – will.mendil May 15 '20 at 15:41

1 Answers1

21

Go modules, which are built into the tooling since Go 1.12 (or 1.11 with a special flag turned on). Create a directory outside of your GOPATH (i.e. basically anywhere), create a go.mod using go mod init (which gives your module a declared importpath), and start working. There's no need to "activate" an environment like with venv; the Go 1.12+ tooling will automatically work within the current module when one is detected, e.g. any go get will happen within module scope.

Although the Go blog entry I linked to mostly focuses on creating a library within a module, which you may want to publish to allow using from elsehwere, this isn't a requirement. You can create a module to hold a program (package main) as well, and there is no need to publish it (although if you do, it becomes possible to use go get to install the program).

hobbs
  • 223,387
  • 19
  • 210
  • 288
  • 1
    The blog post is somewhat outdated now. Its information is correct but incomplete. It doesn't mention module proxies, for instance, because it predates them. A better reference is [the wiki page](https://github.com/golang/go/wiki/Modules). – Peter May 15 '20 at 07:36