Apt-get install golang, next?

Hi, I am new to Go,
What should i do next after install golang on debian 9?
How to add path and add github.com to code?

My first step would be to apt-get remove it and download the latest version from https://golang.org/dl. Unpack it into /usr/local/ (so that it becomes /usr/local/go), add /usr/local/go/bin to your $PATH and you’re good to go.

As a normal user your Go code ("$GOPATH") then by default lives in ~/go. You can go get github.com/someone/whatever and it’ll be downloaded to there and compiled.

7 Likes

Thank you calmh Jakob Borg,
i searched on go and find go in:
/usr/lib/go
/usr/lib/go-1.7
/usr/share/go
/usr/share/go-1.7

When i write echo $GOPATH i didn’t return any thing:
$ echo $GOPATH
#nothing
$ su

echo $GOPATH

#nothing

when i did:
$ go get github.com/jochasinga/golang-book/tree/master/chapter11/math
package github.com/jochasinga/golang-book/tree/master/chapter11/math: cannot download, $GOPATH not set. For more details see: go help gopath

Should i do:
export GOPATH=$HOME/gocode
export PATH=$PATH:$GOPATH/bin

I’d really go with Jakob’s recommendation: apt remove that old version of Go. As you said, you are new to Go, so unless you like unresolved bugs and less performant code, there’s no reason for starting with Go 1.7 instead of Go 1.10

That said, I’m running Debian 9 too. Just unpack the latest version to /usr/local and then add those exports to .bashrc.

4 Likes

To add what Jakob and Ignacio said by default apt will install an older version of go which requires you to setup a GOPATH where you packages will go when you run go get, but if you install a latest version >1.10 then you don’t have to configure GOPATH, it defaults to your home directory for user which makes it much easier so follow what Jakob mentioned above and get a new version of go.

1 Like

thank you all,
i did this tutorial
https://www.tecmint.com/install-go-in-linux/
and worked fine but when i open another linux terminal and write :
$ go version
bash: go: command not found
why?,
sorry for bothering you.

Use gopei shell project to install entire ecosystem. Read here the documentation and take care of prerequisites. Note that a github account is needed for full features.

1 Like

Just add Go to your path. Open /home/your-username/.bashrc and add this to the end of file:

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

The thing is, in Debian 9 (not sure if it’s a general thing or just my configuration, but it may apply to yours too) .profile, .bash_profile or even /etc/profile don’t get picked up when you open a new terminal (at least not by default), but .bashrc does.

1 Like

iegomez, not worked in debian 9

Sorry, my bad, I copy pasted and messed the exports. What you need is this:

export PATH=$PATH:/usr/local/go/bin
export GOPATH="$HOME/go"

sorry not worked

I believe you got it working when sourcing a file where you exported the env vars, right? If so, could you show us what you exported (originally)? Just wondering if something was different between the bashrc and whatever you exported following the tutorial.

Also, I gather that if you execute /usr/local/go/bin/go version directly on a fresh terminal, you do get a correct Go version.

1 Like

I found that I have to add
export PATH=$PATH:/usr/local/go/bin
when I start using golang in any terminal.

Well, the whole point of setting the export in .bashrc (or .profile, .bash_profile, etc., depending on the system) is to not need to export it every time. In your case, I’d really try to check why .bashrc isn’t executed when you open a new terminal, as it is not Go specific. FWIW, and please someone correct me if I’m wrong, .profile is meant to be executed when opening a new login shell, and .bashrc is executed for non-login ones (in Debian). So maybe, if you use something like Guake, you may have some option set for the type of shell that gets opened.

1 Like

iegomez, thank you and forum.golangbridge.org too.

I had issues on Debian too because I’m new to Linux. Then I’ve read this: EnvironmentVariables - Debian Wiki

Graphical logins do not read a shell’s startup files (/etc/profile and ~/.profile and so on) by default, but you as a user may choose to create a ~/.xsessionrc file which does this.

And I created ~/.xsessionrc and added

if [ -f ~/.profile ]; then
. ~/.profile
fi

And it works after logout and login again.

1 Like


this helped me alot

TRY THIS

That project just download and unpack the go compiler, no GOPATH neither else. Not very useful :confused:.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.