Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go - Do I have one workspace for all projects, or one workspace per project?

Tags:

go

When using Go how are projects organized? I'm a bit confused on the workspaces part. I've had a read of the following: https://golang.org/doc/code.html#Workspaces ... and this part has thrown me off a little:

A typical workspace contains many source repositories containing many packages and commands. Most Go programmers keep all their Go source code and dependencies in a single workspace.

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

Or, does it mean you have a main workspace and your projects share those packages?

Bit confused.

like image 279
BugHunterUK Avatar asked Mar 24 '16 13:03

BugHunterUK


3 Answers

So far, I use different workspaces either when I want to use a different version of Go or I want to separate my private work from the code the kids and I have fun with. Also if I want to play with some open source code but want a simple way of cleaning it all up later.

Something like

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 bin, src, pkg subdirectories. I can go get anything I want. Later I can delete the whole temporary directory if I like. Getting things from repositories like github.com has a tendency to get many packages from other contributors, but because it puts them all into a clean src subdirectory, it's easy to use find and see what has been pulled down. And later it is even easier to remove everything from the hd again.

like image 141
WeakPointer Avatar answered Oct 19 '22 20:10

WeakPointer


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

The neat bit is that it integrates with most shells and auto-detect projects as you cd them.

like image 37
Tommaso Barbugli Avatar answered Oct 19 '22 20:10

Tommaso Barbugli


You have one workspace and projects share the packages.

It's there in the overview section:

Go programmers typically keep all their Go code in a single workspace.

Note that this differs from other programming environments in which every project has a separate workspace and workspaces are closely tied to version control repositories.

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

like image 29
user1431317 Avatar answered Oct 19 '22 20:10

user1431317