The GOPATH is for everyone

2 min read Original article ↗

Background

When I started using Golang 3 years ago, I came from a world of Python, PHP, and NodeJS where every time I created a new repository I would have to think about where it was going to go. This would usually result in directory structures like:

.
├── repo1
├── myCode
│ ├── repo2
│ ├── repo3
│ └── repo4
├── myOrg
│ ├── repo5
│ ├── repo6
│ └── repo7
├── opensourceOrg
│ ├── repo10
│ ├── repo8
│ └── repo9
└── scratch
├── repo11
├── repo12
└── repo13

Maybe I’m not disciplined enough, but this tree could look wildly different depending on my mood, the amount of coffee I’ve had, or what task I’m thinking about and working on.

There’s actually information lost here:

  • What organization/user does repo1 belong to?
  • Are we sure that every repository in myCode is ours?
  • What version control system do these repositories use?

As I started learning go, I absolutely hated the idea of the GOPATH, some language is going to tell me how to organize my code? Ridiculous, but I set it up to get up and running with Golang. Now even across my non-Go projects I can’t imagine doing it any other way.

How does the GOPATH help?

The GOPATH is an environment variable that specifies where the Golang tool chain should put and look for Go code (before the adoption of vendoring and gomod). There’s a lot of reasons why the community didn’t like the GOPATH as a global package management system, but I won’t get into that here.

If you have go installed, the get command comes out of the box. For a non-Go project this usually looks like:

> go get github.com/tylfin/geospy
package github.com/tylfin/geospy: no Go files in /Users/tylerfinethy/go/src/github.com/tylfin/geospy

Which will create the tree-structure:

go/
├── bin
├── pkg
└── src
└── github.com
└── tylfin
└── geospy

For multiple repositories, organizations, and version control systems this will look like:

go/
├── bin
├── pkg
└── src
├── github.com
│ ├── tylfin
│ │ ├── dynatomic
│ │ └── geospy
│ └── uudashr
│ └── gopkgs
└── golang.org
└── x
└── tools

That’s it. Go will create a deterministic structure that answers all the questions above, and will be the same across every system.

While I understand it might be suspect to use an entire programming language to organize your code, it’s a quick install and works well enough for me that I’ll be using it for the foreseeable future. I recommend it to all my colleagues as well.