Container Application Development with Go

Quick Start Guide

Introduction

Go (golang) is a very intuitive programming language, which is comparable
to C and creates statically linked binaries for several
architectures. Go is designed for efficient, high performing and low latency
server applications. With Go you can create ARM binaries which run inside a
container without any dependencies. Build, copy and run.

For more information about Go, visit www.golang.org.

Setup Development Environment

Download and setup Go on your computer. You can find binaries for Windows,
Mac OS X and Linux here. If you're
running Linux you can install Go through the package manager, too. On Ubuntu or
Debian try:


sudo apt-get install golang

Point the GOROOT environment variable to your Go installation directory.
Create a "go" sub-directory inside your home directory and set your GOPATH
environment variable to this directory.

Windows example:


C:> set GOROOT=C:\go
C:> set GOPATH=C:\Users\maxmuster\go

Add Go bin directory to your path. On Windows execute:


C:> set PATH=%PATH%;%GOROOT%\bin

Check your Go version:


C:> go version
go version go1.6.3 windows/amd64

Ensure that your running Go version 1.5 or above!

Now you're ready to create your first Go application for a container.

Hello World Application

Change to your go directory inside your home directory and create a "src"
directory:


C:> cd Users\maxmuster\go
C:\Users\maxmuster\go> mkdir src\maxmuster.de\hello

Open your favourite text editor, copy and paste the following source code and
save the file inside the previously created source directory. Name the source
file: hello.go

package main

import "fmt"

func main() {
    fmt.Println("Hello World")
}

You can execute the application on your local computer to check for errors
and that your application runs as considered.


C:\Users\maxmuster\Go> go run src\maxmuster.de\hello\hello.go
Hello World

To build an ARM binary for your container, you need to specify the
target before running the "go build" command. In the case of an container
the environment variable GOOS is set to "linux", GOARCH is set to "arm" and
GOARM is set to "7" for ARMv7 support with hard-floats.


C:\Users\maxmuster\Go> set GOOS=linux
C:\Users\maxmuster\Go> set GOARCH=arm
C:\Users\maxmuster\Go> set GOARM=7
C:\Users\maxmuster\Go> go build src\maxmuster.de\hello\hello.go
C:\Users\maxmuster\Go> dir
19.09.2016 14:23 <DIR> .
19.09.2016 14:23 <DIR> ..
13.09.2016 09:28 <DIR> bin
19.09.2016 14:23 1.854.112 hello
22.07.2016 09:59 <DIR> pkg
19.09.2016 14:06 <DIR> src

Using Linux it is possible to do everything in just one line. Enter the directory
of your hello world project and run:


user@machine ~/hello $ GOPATH=$(pwd -P) GOOS=linux GOARCH=arm GOARM=7 go build hello.go

After successfully building you can see a 1.8 MB large hello file inside your
current directory. You can transfer this file via SCP to your container and
execute it.


root@container-1234 ~ $ ./hello
Hello World!

Your first container application, written in Go, is running. Done!