-2

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
smatthewenglish
  • 2,831
  • 4
  • 36
  • 72
  • 1
    Why would it have anything to do with any programming paradigm? It doesn't even have anything to do with *programming* except in the most boring sense. – hobbs Aug 15 '17 at 05:49

2 Answers2

3

It's an "include path". Practically every (modern) language uses one.

For C/C++, it's the combination of the LIB and INC environment variables (at least in Unix/Makefile environments).

For Perl (5) it's the PERLLIB or PERL5LIB environment variables.

For Python it's the PYTHONHOME environment variable.

For Node.js it's the NODE_PATH variable.

Etc, etc.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • yeah but in python or w/e you don't need to specify it in the same way that you do with golang, you know what I mean? I don't have the right words to describe maybe – smatthewenglish Aug 15 '17 at 12:56
  • I don't know what you mean. In every case, the environment variable must be set, or you can't use the language properly. Go is no different. – Jonathan Hall Aug 15 '17 at 14:02
  • yeah but I've never had to be in my "python path" to make sure something worked properly before – smatthewenglish Aug 15 '17 at 14:05
1

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).

Paul92
  • 8,827
  • 1
  • 23
  • 37