1

I have a problem with my go installation. I'm using Manjaro and I think it is related to this as Manjaro seems to handle the go env differently then suggestest by ubuntu and windows.

I have go installed and can run code as expected:

$ go run gitlab.com/gitlabtest
Hello, GitLab!

Then I check if there any enviroment variables set with:

$ echo $GOROOT

$ echo $GOPATH

$ echo $GOBIN

$

So there are non as it seems which is odd. Why could I run my test program? I try to check for environment variables another way:

$ go env GOROOT
/usr/lib/go
$ go env GOPATH
/home/bluebrown/go
$ go env GOBIN

$ 

That is interesting. Go itself seems to have some knowledge of the environment variables. That's probably why I can run go code from anywhere, effectively targeting $GOPATH. There is just one problem, $GOBIN seem to be unset.

OK, that means I have to edit my ~/.bash_profile I guess.

#
# ~/.bash_profile
#
[[ -f ~/.bashrc ]] && . ~/.bashrc

export GOROOT=/usr/lib/go
export GOPATH=$HOME/go
export GOBIN=$HOME/go/bin

Finally I get the result I want as it seems:

$ source .bash_profile
$ echo $GOROOT &&echo $GOPATH &&echo $GOBIN
/usr/lib/go
/home/bluebrown/go
/home/bluebrown/go/bin
$
$ go env GOROOT &&go env GOPATH &&go env GOBIN
/usr/lib/go
/home/bluebrown/go
/home/bluebrown/go/bin

Thats great now I can use packages from the $GOBIN like glide or govendor right?

[~]$ go get -u -v github.com/kardianos/govendor
github.com/kardianos/govendor (download)
[~]$ cd $GOPATH/src
[src]$ mkdir testdir
[src]$ cd testdir
[testdir]$ govendor init
bash: govendor: command not found

Well maybe not. So I try glide:

$ curl https://glide.sh/get | sh
$...
$which: no glide in  
$ (/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:  
   /usr/lib/jvm/default/bin:/usr/bin/site_perl:
   /usr/bin/vendor_perl:/usr/bin/core_perl:/usr/local/go/bin)

glide not found. Did you add $GOBIN to your $PATH?
Fail to install glide

So it turns out that no matter what pack I have in $GOBIN it can't get found. When I restart the device, everything is reset again for some reason :(

At this point I don't know anymore what to do.

  • 2
    As the error message indicates, have you tried adding the GOBIN variable to your PATH variable? – Haxiel Nov 28 '18 at 13:43
  • What is your question? The title is concise but the body needs to be narrowed down. How to install golang is here golang.org/doc/install. Setting environment variables in bash scripts can be referenced here mywiki.wooledge.org. – justinnoor.io Nov 28 '18 at 13:54
0

Regarding being able to execute Golang binaries you install with go get or go install, you need to add the Golang "bin" directory to your $PATH for that to work.

Note that you don't really need to have a $GOBIN variable set for this to work. If you don't have one, simply use something like this in one of your initialization files (such as ~/.bash_profile):

PATH=$PATH:$HOME/go/bin

(Of course, if you do set $GOBIN then use PATH=$PATH:$GOBIN instead.)


Regarding not having the Golang environment variables set, these days you don't need them for most cases.

The $GOROOT variable points at where the Golang installation was unpacked. If you use the "official" distribution, then it defaults to /usr/local/go, so if you unpack the distribution into that directory, you don't have to set it. Linux distributions usually customize that default to wherever they package Golang files, in Manjaro's case, the more sensible /usr/lib/go. Regardless, if you're using a packaged Golang from a distro, or if you unpack the official one to the default location, you don't need that variable set.

Regarding $GOPATH, it used to be required, but since Golang 1.8 it now defaults to ~/go:

GOPATH can be any directory on your system. In Unix examples, we will set it to $HOME/go (the default since Go 1.8).

So, again, if you're fine with Golang packages (for instance, the ones you get using go get) being stored at the default location, you don't need to set it either.

Finally, $GOBIN defaults to the bin directory under $GOPATH, so you typically don't need to customize it either.

As you noticed, you can use go env to check what Golang's notion of those variables are, if you leave them unset, in which case Golang should use the defaults described above.

Of course, if you want to refer to these variables from the shell, then you need to set them! The fact that Golang has defaults for these concepts means it will be able to work with the variables unset... Not that it will magically inject those into your environment. And, once again, if you want Golang binaries in your $PATH, you'll have to set that up, see the instructions at the top of the answer.

  • 1
    Thank you so much for this very Comprehensive answer! As a beginner thought somehow that adding the GOBIN to $PATH is specifying the $GOBIN variable as I did. Now I know it means something different. Thank you again, I'm Glad it was such a small issue. Everything is working as I want now, without even setting the variables as suggested by you :) – The Fool Nov 28 '18 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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