1 Build Golang Programs
Stefan Naumann edited this page 1 year ago

On building Programs written in Go

With go modules this has gotten way easier. This page does not reflect the change yet.

Go is quite a neat language, but it requires a set-up to build any programs written in Go. When you've ever used go, you will know, that there is a $GOPATH directory, where the compiler expects your code to be placed (except when using go-modules). This folder can be essentially empty, with only your code in it, in a src-directory.

But mvoCI has no notion of $GOPATH, it just places your code in the repo-folder and calls your Build-Skript to build the code. For building go-code I use this set-up:

export GOPATH=$(pwd)
mkdir -p src/mvoCI

# move the code to the go-dir
shopt -s extglob dotglob
mv !(src) src/mvoCI/
shopt -u dotglob

pushd src/mvoCI
# install build dependencies
make dep

# build
make dist
# cleanup
rm -rf web hook log Makefile main.go mvo.cfg static repo static views core build builds
popd

# cleanup for packaging
mv src/mvoCI/dist/* .
rm -rf src pkg

Creating a $GOPATH

You start out, mkdiring an empty directory and setting the $GOPATH-variable to the current working directory. That means, that go expects a src-directory (which by coincidence already exists in the mvoCI-code) and a pkg-folder for libraries. The the code (from the src/-directory) is moved into src/mvoCI. mvoCI is the package name used within the code. Go uses the directories in src as identifiers of packages.

Then we change into the src/mvoCI-directory with pushd (i.e. allowing us to leave the directory again with popd).

Installing Dependencies

Go dependencies come in the form of a github-repository. With the command line call go get ... one can install third party libraries. In my Makefiles for Go-projects I always include a make dep call, which does exactly that. (go-modules might solve that, I haven't had a look into them).

Cleanup (optional)

Although not necessary, I clean up all unnecessary code, directories, etc. as I use mvoCI to zip my builds. If you're not planning on doing that, or want to keep log-files or intermediate files in the archives, you don't need to do that.

Getting a new-ish Go-Compiler

If your distribution does not serve a recent Go-Compiler and a feature is missing, which you need in your program, you can always download a standalone-package from golang.org. Be sure to then include this Go-package into your $PATH and set the $GOROOT variable to the directory where you placed the Go-package.