Skip to main content

Golang

You can see a full example of using Batect with Golang in the Golang sample project.

The Golang bundle provides a container with all of these options pre-configured.

Example configuration

batect.yml
containers:
build-env:
image: golang:1.15.3-stretch
volumes:
- local: .
container: /code
- type: cache
name: go-cache
container: /go
working_directory: /code
environment:
# With the image above, GOPATH defaults to /go, so we don't need to set it explicitly.
GOCACHE: /go/cache

Caching dependencies

tip

tl;dr: Mount a cache into your container for your GOPATH and GOCACHE, otherwise you'll have to download and compile your dependencies every time the build runs

GOPATH

Golang caches the source for dependencies under GOPATH. By default, this is at $HOME/go. However, because Batect destroys all of your containers once the task finishes, this directory is lost at the end of every task run - which means that Golang will have to download all of your dependencies again next time you run the task, significantly slowing down the build.

The solution to this is to mount a cache into your container for your GOPATH. This cache persists between task invocations, which means that any downloaded dependencies will be available next time the task runs.

For example, the official Golang Docker images set GOPATH to /go, so mounting a cache at /go inside the container will allow your dependencies to be persisted across builds.

The example configuration above demonstrates how to do this.

GOCACHE

The Golang compiler caches intermediate build output (such as built libraries) in GOCACHE. Just like for GOPATH, the contents of GOCACHE will be lost when the task finishes and the container is removed, which means that Golang will have to recompile all code for your project, even if it has not changed. This can also significantly slow down the build.

Again, the solution is to mount a cache into your container for your GOCACHE.

The official Golang Docker images do not set a default for GOCACHE, so you will need to set this yourself.

In the example above, GOCACHE has been placed inside /go (which is the default GOPATH) so that both use the same cache.

Using golangci-lint

tip

tl;dr: Mount a cache into your container at ~/.cache/golangci-lint/, otherwise you'll experience slow performance from golangci-lint

golangci-lint caches build artefacts used during analysis to speed up subsequent runs. By default, it stores this cache at ~/.cache/golangci-lint/.

Without a mounted cache, this directory would be lost every time Batect starts a new container, causing golangci-lint to need to rebuild your application every time you run it.

The solution to this is to mount a cache into your container for ~/.cache/golangci-lint/. For example, assuming the home directory for your container's user is /home/container-user:

batect.yml
containers:
build-env:
# ... other configuration omitted for clarity
volumes:
- type: cache
name: golangci-cache
container: /home/container-user/.cache/golangci-lint

Subscribe to the Batect newsletter

Get news and announcements direct to your inbox.