Go Do I have one workspace for all projects or one workspace for each project?

When using Go how are projects organized? I got a little confused about workspaces. I read the following: https://golang.org/doc/code.html#Workspaces ... and this part threw me a little:

A typical workspace contains many source repositories containing many packages and commands. Most Go programmers save all Go source code and dependencies in one workspace.

Does this mean that for each project that I create, this is a separate workspace? For example, if two projects use the same package, I will have two instances of this package on my computer.

Or does this mean that you have a main workspace and your projects exchange these packages?

Bit confused.

+5
source share
3 answers

You have one workspace, and projects share packages.

It is located in the overview section:

Programmers

Go usually saves all their Go codes in one workspace.

Note that this differs from other software environments in which each project has a separate workspace and workspaces are closely related to version control repositories.

Change If you use vendoring , you can effectively get a separate workspace for each project. This brings us closer to how other programming languages ​​work.

+4
source

I personally like to have a different GOPATH for each project. If you use a tool to automate the process, you can use vg , which will take care of managing different GOPATHs for your projects.

The clean bit is that it integrates with most shells and automatically detects projects as you cd them.

+6
source

Until now, I use different workspaces either when I want to use a different version of Go, or I want to separate my personal work from the code with which children and I are happy. Also, if I want to play with some kind of open source, but want an easy way to clean it up later.

Sort of

 mk /tmp/tmpgo cd /tmp/tmpgo # Copy or edit a setenv file . setenv # I use bash 

The setenv file looks something like this.

 export GOROOT=$HOME/go16 export GOPATH=$PWD export GOBIN=$GOPATH/bin export PATH=$GOROOT/bin:$GOPATH/bin:$PATH export PS1='\[\033[01;32m\]workspacenamehere\[\033[01;33m\] \W\[\033[00m\] ' 

This gives me a go workspace with its own subdirectories bin, src, pkg. I can get whatever I want. Later I can delete the entire temporary directory if I want. Getting things from repositories such as github.com tends to get a lot of packages from other members, but because they put them in a clean src subdirectory, it's easy to use find and see what has been taken down. And then even easier to remove everything from hd again.

+3
source

Source: https://habr.com/ru/post/1245700/


All Articles