54

I am new to Go programming language and every tutorial starts off from setting GOPATH to current project folder.

Am I missing something? Is programmer really supposed to set GOPATH manually when he cd to his new Go project folder? I have read several FAQ entries about GOPATH but still couldn't wrap my head around it.

And why does GOROOT exist then? What's its purpose?

Are there any automatic tools which detects if current directory is root folder of Go project (for example by some hidden file) and changes GOPATH to this directory automatically?

Thank you, any advice really appriciated

ps. For example I develop completely disjoint Go projects A, B and C, should they live in single "workspace" environment? I guess not, but what I should do with GOPATH and GOROOT then?

2
  • 6
    GOROOT points to your Go installation, GOPATH points to the root of your workspace. Go can't infer these automatically by design so you have to set them. Generally it is fine to have multiple projects in one GOPATH, as each project resides in a different subfolder (by design).
    – fuz
    Jun 19, 2014 at 12:12
  • The above comment is the correct answer, as the person is not asking what is the purpose of GOPATH , but why it should be there at all. Feb 13, 2020 at 9:12

3 Answers 3

42

The goal of the GOPATH is to centralize all packages into one common workspace. It is not really a new concept by itself (think of the Java Classpath for example), but Go's use is drastically simpler by not supporting packages versioning.

The Go programmer isn't supposed to set GOPATH manually when entering a new project folder. Each project folder is supposed to be a package by itself, and reside in the GOPATH along other packages, so GOPATH should be set only once. Tutorials begin by setting the GOPATH in order to isolate the tutorial workspace from anything else (or simply assuming that a user hasn't set the GOPATH, yet).

GOROOT is set to provide the standard packages to the Go programmer, you don't need to do anything with it. In short, there is a single rule for GOROOT: never, ever, touch it. Don't install anything in it, don't modify standard packages, etc.

I'm not aware of a tool to detect Go projects in the current directory, but it shouldn't be highly complex to create.

How you handle different projects is up to you. The Go way is to put every project as a package in the $GOPATH/src directory and do everything from there. As I don't really like it, I defined my GOPATH to be $HOME/.go. Then I put each project in a dedicated directory somewhere else (anywhere in my computer), and symlink the project directory into my $GOPATH/src dir. I can then use every Go toolchain command (e.g. go build myproject), use the project as package for another one, etc.

10
  • 2
    You can't "freeze" the project dependencies. Go philosophy is that everything should be backward compatible. If you release a library, you have to ensure it stay compatible with older versions's public API. If you need to break the API, then it is a new package and should have a new import path. To lock a package, the common way is to make a local copy (by forking it for example).
    – Elwinar
    Jun 19, 2014 at 13:14
  • 2
    Go does support limited versioning concurrent with Go releases. Tagging a commit with Gox.x[.x] (e.g. Go1.2 or Go1.2.2) will make go get always check out the branch matching the release tagged the same as go version lists instead of the head. It's "supposed to" be used for packages including features added in later Go releases (e.g. 1.3 added sync.Pool), but many package developers will only break API on Go version changes and abuse this fact.
    – Linear
    Jun 19, 2014 at 14:10
  • 4
    @dig, this is commonly called "vendoring" on the Go nuts mailing list. There's a multitude of third-party tools for doing this, as well as the gopkg.in service being an alternative solution approaching the problem from another angle. Be sure to read this and this and search the ML for words "package management".
    – kostix
    Jun 19, 2014 at 17:06
  • 4
    @dig, in short, Go currently does not have a package management tool, and go get is not it (a common misconception is to treat go get as a package manager). There's a bunch of tools dealing with the problem; so do your research, look at some of them, pick your favorite and use it.
    – kostix
    Jun 19, 2014 at 17:08
  • 4
    @kostix Good to know, thank you for guidance. I was confused because I thought that go get is meant to be the package manager
    – Nik
    Jun 20, 2014 at 13:17
12

GOPATH allows you to collect dependency source code and the resulting compiled binaries in one place. This seems like a really attractive idea. However, I found myself working on several totally unrelated Go projects and an alternative approach suited me better.

This is a similar but different strategy to Elwinar's symlnks. I start a new project in an empty folder and create src. And I drop into the folder this shell script called env.sh:

if [ `type -p go` = "" ]; then
    export PATH=$PATH:/usr/local/go/bin
fi
export GOPATH=$PWD
export PATH=$PATH:$PWD/bin

Each time I start work, I use

. env.sh

Note the dot and space - they matter.

Now, everything I do on this project is localised within this folder. It's possibly not the most widely-used strategy, but it works well for me.

And another thing: if your dependencies make use of environment variables for testing etc, you can put them in env.sh too. For example, Gorp has

export GORP_TEST_DSN=test/testuser/TestPasswd9
export GO_TEST_DSN=testuser:TestPasswd9@/test

Addendum

In the most recent Go versions, GOPATH is optional; if you don't set it, the default is $HOME/go. If you do set it and also want to use the new modules feature, set GO111MODULES=on also.

1
  • 1
    That looks like the way I am used to, I think I would pick it (because I like when my projects are completely independent from each other, and classy "workspace" idea of GOPATH reminded me days when I had dozens of "subdomain" folders in Apache's www and there was no convenient way to split things without breaking something occasionally)
    – Nik
    Jun 20, 2014 at 13:22
10

You don't need to set your GOPATH or GOROOT. GOPATH by default is under your user/home directory.

If no GOPATH is set, it is assumed to be $HOME/go on Unix systems and %USERPROFILE%\go on Windows. If you want to use a custom location as your workspace, you can set the GOPATH environment variable.


Go Modules

Now there's Go Modules support (since Go 1.11), so you don't have to use GOPATH anymore. For example, you can go to any directory on your system (outside of $GOPATH), and you can initialize a new Go module there, then you start working there. No GOPATH is needed.

You just need to do this once (while in a directory):

go mod init

$GOPATH: Go stores these files under it:

  • Source files ($GOPATH/src)
  • Compiled package files ($GOPATH/pkg)
  • Runnable files ($GOPATH/bin)

$GOROOT: Where the Go source code resides like Go Standard Library.


Also to run any go installed executable file from anywhere on your system, you might want to add $GOPATH/bin to your path environment variable like this:

export PATH=$PATH:$(go env GOPATH)/bin

More information check out this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.