You can see a full example of using batect with Golang in the Golang sample project.
containers: build-env: image: golang:1.14.0-stretch volumes: - local: . container: /code options: cached - 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
Mount a cache into your container for your
GOCACHE, otherwise you'll have to download and compile your dependencies every
time the build runs
Golang caches the source for dependencies under your
GOPATH. By default, this is at
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 that persists between builds into your container for your
For example, the official Golang Docker images set
/go, so mounting a cache at
/go inside the container will
allow your dependencies to be persisted across builds.
The Golang compiler caches intermediate build output (such as built libraries) in
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 that persists between builds into your container for your
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
/go (which is the default
GOPATH) so that both use the same cache.