Search code examples
gogopath

What is the programming paradigm behind the $GOPATH, what does it do?


As someone new to Golang, I'm somewhat perplexed by the notion of the $GOPATH.

The only thing in my experience that it reminds me of is the "System Folder Path" (C:\Windows, C:\Program Files) on Microsoft Windows machines- is it somehow conceptually related to this?

I've seem a description of it from the go team, but it's too practical, it talks about what it is, as opposed to why it is.

So then, why is it?


Solution

  • GOPATH is a variable that indicates where the dependencies of your application are installed. It is basically a path to a directory where you store the packages your application might use.

    Any application of a reasonable size has dependencies. In golang, these come in the form of packages. At compile time, the location of the dependencies (i.e. packages you use) needs to be known such that your executable can be built.

    They can either be stored at a fixed, predefined location or make somehow the user be able to specify the location himself. The first approach has a lot of downsides (for example, it would be impossible to support operating systems with different directory structure). Thus, the designers of the go tool decided to make it user configurable by the means of this variable. This also gives the user much more flexibility, such as the ability to group the dependencies for different projects in different directories.

    The usage of a environment variable (as GOPATH) is not limited to golang. Java has its CLASSPATH, Python its PYTHONPATH and so on (each of them with their quirks, but with the same basic role).