Install go (golang) on Raspbian

heiko picture heiko · Jan 19, 2018 · Viewed 18.8k times · Source

I checked diverse forums but I still did not make it working. I like to install go (golang) on my Raspberry PI - Raspbian:

With

sudo apt-get install golang

I installed go and with

export GOPATH=$home/pi/gocode

i set the GOPATH so i tryed to install from a homepage a new program with (sudo go get -u github.com/....) but, I only get "cannot download, $GOPATH not set. For more details see: go help gopath".

I really get crazy for my studip simple mistake that i do not see.

I would be pleased if i get a very detailed "how to do" discription since I am new to Linux and Raspbian, so everything which is made for real dummys should be good enough for me. Thank you for your help.

Answer

Peter Gloor picture Peter Gloor · Jan 30, 2018

This are detailed instructions about how to install Go on Raspbian Stretch from the repositories.

As of today, 2018-01-30, this will install Go 1.7. The most actual version for manual installation from the downloads is Go 1.9.3.

I. Login to your user on the Raspberry Pi (I'm using the default user pi).

II. Install Go (golang)

pi@pi3-2:~ $ sudo apt update 
pi@pi3-2:~ $ sudo apt install golang

III. Create a working directory for all your go projects in your $HOME directory. It's best to name it go, as this defaults to the GOPATH in future Go versions (starting with Go 1.8).

pi@pi3-2:~ $ mkdir go

IV. Append the $GOPATH environment variable and modified PATH settings to your .profile

pi@pi3-2:~ $ echo 'export GOPATH=$HOME/go' >> ~/.profile
pi@pi3-2:~ $ echo 'PATH="$HOME/go/bin:$PATH"' >> ~/.profile

V. Logout and Relog with the new settings then check your settings

pi@pi3-2:~ $ go env

GOARCH="arm"
GOBIN=""
GOEXE=""
GOHOSTARCH="arm"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/pi/go"
GORACE=""
GOROOT="/usr/lib/go-1.7"
GOTOOLDIR="/usr/lib/go-1.7/pkg/tool/linux_arm"
CC="gcc"
GOGCCFLAGS="-fPIC -marm -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build187598155=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"

Especially make sure GOPATH points to your previously created Go working directory. Don't care about setting GOBIN as mentioned in some documentation. It's usually not necessary and Go will automatically use $GOPATH/bin/ for your Go installs.

However, you might also want to check the path settings (/home/pi/go/bin should be included) to make sure you can run the code you installed with go install.

pi@pi3-2:~ $ echo $PATH
/home/pi/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games

VI. A few words about the Go working directory structure

Over time, the Go working directory will contain three sub-directories: bin, src and pkg. Except src they will be automatically created, when needed the first time. The structure for user pi will look like this:

/home
  /pi
    /go
      /src
      /pkg
      /bin

bin will contain all Go executable's you have installed using go install command.

pkg will contain all compiled packages that can be imported into your projects.

src will contain all your source files, either your own or sources downloaded from external repositories.

For eksample the command go get github.com/petergloor/hello-go will automatically fetch and place the source files from the corresponding external Github repository into the local directory $HOME/go/src/github.com/petergloor/hello-go.

As it's rather common to fetch external repositories either for reference or contribution it becomes important to keep your directory structure always well organized and clean.

Apart from that you are free to organize your projects as long they are hierarchically structured below the $HOME/go/src/ directory and follow the rules mentioned in the documentation.

However, to clearly organize my projects I personally always place my projects into $HOME/go/src/github.com/my-github-account even if I don't have an external repository for it.

If you don't have a github account you can likewise use any other external repository account.

As I mentioned, even it's not needed at all I prefere to use my Github account to clearly identify my projects. And even it's not needed I will use the username pi to distinct the user from other project maintainers in the following example.

VII. So let's add a "hello world" project to test our installation.

a) First let's create the project folder and cd into its directory.

pi@pi3-2:~ $ mkdir -p $HOME/go/src/pi/helloworld
pi@pi3-2:~ $ cd $HOME/go/src/pi/helloworld
pi@pi3-2:~/go/src/pi/helloworld $

b) With an editor of your choice create a file main.go with the following content

// helloworld project main.go.
package main

import ("fmt")

// main is the entrypoint of the application.
func main() {
  fmt.Println("Hello world! Greetings from Raspberry Pi")
}

Spacing doesn't matter at this point. Go provides a nice tool to do this for you.

c) Now try to run the program.

pi@pi3-2:~/go/src/pi/helloworld $ go run main.go
Hello world! Greetings from Raspberry Pi
pi@pi3-2:~/go/src/pi/helloworld $

In case you get an error, fix it! Carefully, check the spelling and cases (Go is case-sensitive).

d) Next lets format the code:

pi@pi3-2:~/go/src/pi/helloworld $ go fmt

Without a file name this will properly (re-)format all source files within this directory and below.

e) Next let's build helloworld as an executable procram, within this directory.

pi@pi3-2:~/go/src/pi/helloworld $ go build
pi@pi3-2:~/go/src/pi/helloworld $ ls
helloworld  main.go
pi@pi3-2:~/go/src/pi/helloworld $

f) Now you can run it.

pi@pi3-2:~/go/src/pi/helloworld $ ./helloworld
Hello world! Greetings from Raspberry Pi
pi@pi3-2:~/go/src/pi/helloworld $

g) Finally let's install the program into the $HOME/go/bin/ directory.

pi@pi3-2:~/go/src/pi/helloworld $ go install
pi@pi3-2:~/go/src/pi/helloworld $ ls $HOME/go/bin
hello-go  helloworld
pi@pi3-2:~/go/src/pi/helloworld $

h) If everything is done right it can be run by our pi user from anywhere by just entering the name of the command.

pi@pi3-2:~/go/src/pi/helloworld $ helloworld
Hello world! Greetings from Raspberry Pi
pi@pi3-2:~/go/src/pi/helloworld $ cd ~
pi@pi3-2:~ $ helloworld
Hello world! Greetings from Raspberry Pi
pi@pi3-2:~ $

Congratulations!