Loading...
 

Cross-compiling "Hello World" Go program for ESPRESSObin

In this short tutorial we will cover installing Go locally on your Linux machine and cross-compiling a simple Go "Hello World" program for ESPRESSObin running Ubuntu (either 14.04 or 16.04).

Installing Go

To install Go locally on your Linux machine, visit the official Go downloads page and find the binary URL for your system. Make sure to position yourself into your home directory where we will download the tarball using curl:

espressobin@buildserver:~$ curl -O https://dl.google.com/go/go1.10.1.linux-amd64.tar.gz


and then we verify the tarball using sha256sum to make sure it matches the one listed on the downloads page:

espressobin@buildserver:~$ sha256sum go1.10.1.linux-amd64.tar.gz 
72d820dec546752e5a8303b33b009079c15c2390ce76d67cf514991646c6127b  go1.10.1.linux-amd64.tar.gz


Now we will untar the tarball:

espressobin@buildserver:~$ tar xvf go1.10.1.linux-amd64.tar.gz


which should leave us with a ./go directory inside your home directory.

Configuring Go

Change the owner and group of this directory to root and move it to /usr/local (officially recommended location):

espressobin@buildserver:~$ sudo chown -R root:root ./go
espressobin@buildserver:~$ sudo mv go /usr/local


Next thing we need to do is set correct Go paths. As root open .profile config file:

espressobin@buildserver:~$ sudo vim ~/.profile


and in there set Go's root value according to our setup from before ($HOME/espresso_go will be our Go workspace directory):

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


and refresh your profile with:

espressobin@buildserver:~$ source ~/.profile


Make the directory for your Go workspace as you have set it in your .profile:

espressobin@buildserver:~$ mkdir $HOME/go_espresso


This is where Go will build its files.

Cross-compiling "Hello World" program

Next we will make an example "Hello World" Go file which we will store inside our go_espresso directory:

espressobin@buildserver:~$ mkdir -p $HOME/go_espresso/src/hello
espressobin@buildserver:~$ vim $HOME/go_espresso/src/hello/hello.go


and in there we will paste the following code below:

package main

import "fmt"

func main() {
    fmt.Printf("hello, world\n")
}


This program will print "hello, world" if successfully run. To cross-compile the program for ESPRESSObin we issue go build with the correct $GOOS and $GOARCH environment variables for ESPRESSObin:

espressobin@buildserver:~/go_espresso$ GOOS=linux GOARCH=arm64 go build -v src/hello/hello.go
runtime/internal/sys
runtime/internal/atomic
runtime
errors
internal/race
sync/atomic
unicode/utf8
math
sync
syscall
io
strconv
time
reflect
os
fmt
command-line-arguments


after this we should have a binary named hello in our current directory. Check that the program has the correct file type:

espressobin@buildserver:~/go_espresso$ file hello
hello: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, with debug_info, not stripped


Finally we connect to the ESPRESSObin console, transfer the binary to it (e.g. via http-server), make it executable and run it:

root@localhost:/home# wget http://192.168.22.37:8080/hello
root@localhost:/home# chmod +x hello
root@localhost:/home# ./hello
hello, world


and that is it, you have successfully cross-compiled a "Hello World" program for ESPRESSObin using Go.