2

In Go, the workspace contains the src, pkg and bin directories. How do I create multiple projects in the workspace, each with its own src, pkg, bin directories, such that I can 'go get' packages into the pkg directory of a particular project.

zzzz
  • 87,403
  • 16
  • 175
  • 139
tldr
  • 11,924
  • 15
  • 75
  • 120
  • As @jnml pointed out, the way the doc defines workspace is confusing. I prefer to think of the src directory as the workspace. This answered my question perfectly: http://stackoverflow.com/a/9986574/1375688 – tldr Sep 08 '13 at 18:23

3 Answers3

2

You probably do not need that. Let's forget also the word "workspace" it's probably only confusing you.

If you set your GOPATH environment variable that that's all you actually need to have multiple projects independently sitting on you hard disk.

For example, having export GOPATH="$HOME", and performing

$ go get github.com/foo/bar
$ go get github.com/baz/qux

Your directory tree will be

$GOPATH/pkg...
        compiled packages
$GOPATH/src/github.com/foo/bar
        bar.go
$GOPATH/src/github.com/baz/qux
        qux.go

More details here. Note that it does talk about workspaces, but I still consider that fact very unfortunate. The earlier versions of that doc did not use nor define the concept and they were useful anyway. That's IMO a proof of it (the workspace) being redundant.

zzzz
  • 87,403
  • 16
  • 175
  • 139
  • So I can just import "$GOPATH/src/github.com/foo/bar" in any file? – tldr Sep 05 '13 at 14:18
  • @tldr: Drop the `$GOPATH/src/` part from the import statement and you're good to go ;-) – zzzz Sep 05 '13 at 14:30
  • But that would fetch it from the master branch in the github repo. What if I want to use the version that I got from 'go get'? – tldr Sep 05 '13 at 14:34
  • @tldr: Then you have to import your fork which, of course, has a different import path because is sits in a different node of you `$GOPATH/src/` tree. Have you read the above linked document "How to Write Go Code"? – zzzz Sep 05 '13 at 14:36
  • I see. I haven't read that document. Thanks for pointing me to it. – tldr Sep 06 '13 at 00:27
  • I read the document, and I'm a bit unclear about something. If I import github.com/foo/bar, does it download it every time I compile (even if the binary is already there)? – tldr Sep 08 '13 at 02:14
  • @tldr: Only `$ go get github.com/foo/bar` ever downloads. No other go command does that, so it's under your explicit control. – zzzz Sep 08 '13 at 08:16
  • I see. So the actual 'workspace' is my src directory, and the directories inside it are my projects. When I build the projects, their binaries are placed in the corresponding folders inside pkg and bin directories. For example, if I have a directory 'hello' inside the src directory, I can think of that as my 'hello' project. When I build and install, binaries for the main package go in bin/hello and for others in pkg/hello. Is that correct? – tldr Sep 08 '13 at 18:20
0

go get is not intended to be used that way.

all go get packages land in $GOPATH/* as described here: http://golang.org/doc/code.html#remote and there is no concept of separate workspaces.

mnagel
  • 6,729
  • 4
  • 31
  • 66
0

If you really want several "workspaces": Have several entries in GOPATH (separated by ":" on unix). (But most just keep everything under one GOPATH).

Remember that go get fetches packages only into your first GOPATH entry.

The other entries can be used as "seperate workspaces".

Volker
  • 40,468
  • 7
  • 81
  • 87