-2

I'm setting the $GOPATH using export GOPATH=$HOME/go as per GoLang's instructions and everything works fine. When I echo out the path it shows what I set it to.

However, if I close my terminal and re-open it the $GOPATH is no longer what I set it to.

I guess my question is how can I make the new $GOPATH persist?

tommyd456
  • 10,443
  • 26
  • 89
  • 163
  • Where exactly do you set it? In ~/.bashrc? – Ainar-G Apr 16 '15 at 15:47
  • No I'm not using a .bashrc - I'm not sure how to do this part. Could you show me? – tommyd456 Apr 16 '15 at 15:48
  • Are you using Ubuntu? – Ainar-G Apr 16 '15 at 15:51
  • yes i am using Ubuntu – tommyd456 Apr 16 '15 at 15:52
  • possible duplicate of [how do I SET the GOPATH environment variable on Ubuntu? What file must I edit?](http://stackoverflow.com/questions/21001387/how-do-i-set-the-gopath-environment-variable-on-ubuntu-what-file-must-i-edit) – Ainar-G Apr 16 '15 at 15:53
  • see the answer in the question I've linked above. – Ainar-G Apr 16 '15 at 15:54
  • i've seen that - I can't see the answer - there is no accepted anwser – tommyd456 Apr 16 '15 at 15:55
  • This is a Linux/bash question and it's because export only sets the env variable for the current session... – evanmcdonnal Apr 16 '15 at 16:03
  • any chance you could show me how to write it to a file then? – tommyd456 Apr 16 '15 at 16:05
  • @tommyd456 I've provided a step by step answer. Not sure what your skill level with Ubuntu is but these as I noted this is Linux/bash and nothing to do with Go so you don't want to look on SO for future reference. SuperUser or Ubuntu stackexchange for example are far more appropriate for the type of answer I've provided. – evanmcdonnal Apr 16 '15 at 16:11
  • appreciate that - very low by the way (skill level) ha ha – tommyd456 Apr 16 '15 at 16:12
  • related to [$GOPATH value keeps reseting to empty during new sessions of the terminal](http://stackoverflow.com/questions/28006265/gopath-value-keeps-reseting-to-empty-during-new-sessions-of-the-terminal) – Dave C Apr 16 '15 at 16:41

3 Answers3

0

Open you terminal (you can use any text editor, like: gedit, vi or vim).

   gedit ~/.bashrc

go to the end of file and edit with the following lines:

   export GOPATH=$HOME/go
   export PATH=$PATH:$GOPATH/bin

Assuming that /usr/lib/go is your GOROOT

to know your GOROOT you can do:

   go env

or

   which go
  • setting GOROOT allows you to put $GOROOT/bin in your path. There's no problem in set GOROOT – Felix Ribeiro Apr 16 '15 at 16:01
  • 2
    From many posts on the mailing list: "In general, never set GOROOT." Also see: [You don’t need to set GOROOT, really](http://dave.cheney.net/2013/06/14/you-dont-need-to-set-goroot-really). Only set it if you absolutely need to, otherwise it will eventually cause problems. – JimB Apr 16 '15 at 16:07
  • Ok, fixed. I can't really see BIG problems, but ok. – Felix Ribeiro Apr 16 '15 at 16:12
  • 1
    Maybe it's only a big problem for those of us who regularly answer questions on the ML caused by setting GOROOT (and those affected of course) ;) – JimB Apr 16 '15 at 16:14
0

Just so you know this should be answered in general form on SuperUser or something other than here. Regardless here's the steps to make it so this is always set in your session;

cd ~
vi .bashrc
//page down, end whatever to get to bottom of file if it's not new
press i to insert, add export GOPATH=$HOME/go
press escape
:w
:q
evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115
0

To make Felix's answer more simpler you could run the below commands while not breaking your head with vim

echo "export GOPATH=$HOME/go" >> ~/.bash_profile
echo "export PATH=$GOPATH/bin:$PATH" >> ~/.bash_profile
source ~/.bash_profile