---
title: "How to speed up Go CI"
description: "Speed up Go CI by keeping the build and test caches warm, sharing them with GOCACHEPROG, caching golangci-lint, and splitting packages across jobs."
url: https://9apes.com/blog/speed-up-go-ci/
---

# How to speed up Go CI

To speed up Go CI, keep Go's build and test caches warm between runs, make sure their keys actually hit, give golangci-lint its own cache, and split large test suites by package. Go compiles quickly, and its test cache can skip unchanged packages entirely. Most slow Go pipelines are slow because those caches miss, not because the compiler is slow.

This post is part of a series on [speeding up CI](/blog/speed-up-ci-builds/). The hub covers techniques that apply to every language; this one covers the Go toolchain.

## Know what Go caches

The `go` command keeps two caches:

- **The module cache** (`GOMODCACHE`) holds downloaded dependency source. It only changes when `go.mod` or `go.sum` change.
- **The build cache** (`GOCACHE`) holds compiled packages and test results. A package whose inputs did not change is not recompiled, and a test whose package, flags, and the files and environment variables it read did not change is not re-run.

On an ephemeral runner, both start empty unless you restore them. A cold `go test ./...` compiles the standard library dependencies, every module, and every package, then runs every test.

## Use setup-go's cache, and check its key

[`actions/setup-go`](https://github.com/actions/setup-go) caches both directories by default:

```yaml
- uses: actions/checkout@v7
- uses: actions/setup-go@v7
  with:
    go-version-file: go.mod
    cache-dependency-path: go.sum
- run: go test ./...
```

Since v6, the default cache key comes from `go.mod`. Setting `cache-dependency-path: go.sum` ties it to the exact dependency versions instead.

That key does not include your source code. setup-go's [advanced usage docs](https://github.com/actions/setup-go/blob/main/docs/advanced-usage.md) note that the build cache is not updated based on source changes, and that adding `**/*.go` to the key only creates churn. In practice, a cache saved early on `main` keeps being restored long after your packages have changed, and its build-cache hit rate drifts down. Two common fixes:

- Save from `main` only, and let the key rotate, for example by including the week number, so the stored build cache stays recent.
- Use `actions/cache` directly with `restore-keys`, which restores the newest entry for a prefix, and save a fresh entry on every `main` build.

Always compare restore time with build time. A large `GOCACHE` archive can take longer to download and extract than a fast machine takes to rebuild it.

## Keep cache keys stable

Go's build cache keys include more than source contents. Two things cause unexpected misses in CI:

- **Absolute paths.** Without `-trimpath`, a package's cache key includes its directory. If your checkout path differs between runners or jobs, the cache misses. `-trimpath` removes paths from the key, but it is itself part of the key, so builds with and without it do not share entries. Pick one and use it everywhere.
- **Flags and environment.** Different `-tags`, `GOFLAGS`, `CGO_ENABLED`, or `GOARCH` values produce different cache entries. Lint, test, and build jobs that use different settings cannot reuse each other's work.

Tests that read environment variables or files are only cached while those stay the same. A test that reads a timestamp file or a random environment value on every run will never be cached. `go test -count=1` is the [documented way](https://pkg.go.dev/cmd/go) to bypass the test cache on purpose; remove it from CI if it is there by habit.

## Share the cache with GOCACHEPROG

Since [Go 1.24](https://go.dev/doc/go1.24), `GOCACHEPROG` lets the `go` command delegate its build and test cache to an external program. Instead of downloading a whole cache archive at the start of a job, the program fetches and stores individual entries, typically in an object store, as the build needs them.

That changes the economics for large repositories: jobs share one cache across branches and runners, nothing is restored that the job does not use, and the cache never goes stale because every job writes to it. Open-source implementations include Brad Fitzpatrick's reference [go-tool-cache](https://github.com/bradfitz/go-tool-cache) and several S3- and cloud-storage-backed tools. You run the storage, so weigh the setup against your cache sizes before adopting it.

## Cache golangci-lint separately

golangci-lint runs dozens of analyzers that type-check your code and keep their own cache. The official action caches it and relies on setup-go for modules:

```yaml
- uses: actions/setup-go@v7
  with:
    go-version-file: go.mod
- uses: golangci/golangci-lint-action@v9
  with:
    version: v2.15
```

The [action](https://github.com/golangci/golangci-lint-action) invalidates its cache every seven days by default. `only-new-issues: true` reports only issues introduced by the pull request, which makes adopting new linters easier, but it filters output rather than reducing analysis time.

Linting is often the slowest Go job. Run it in parallel with tests, not before them.

## Split large test suites by package

`go test ./...` already builds and runs packages in parallel, up to `GOMAXPROCS` at a time (the `-p` flag), and runs `t.Parallel()` tests concurrently within each package (the `-parallel` flag). On a single machine, more cores help directly.

For suites too large for one machine, Go has no built-in sharding. A common approach is to split the package list across a matrix:

```yaml
strategy:
  matrix:
    shard: [0, 1, 2]
steps:
  - uses: actions/checkout@v7
  - uses: actions/setup-go@v7
    with:
      go-version-file: go.mod
  - run: |
      go list ./... | awk "NR % 3 == ${{ matrix.shard }}" > packages.txt
      go test $(cat packages.txt)
```

Splitting by package count balances poorly when one package holds most of the slow tests. [gotestsum](https://github.com/gotestyourself/gotestsum) reads `go test -json` output and can report per-package durations, which help you rebalance shards by hand.

## Stay on a current Go release

Recent releases have improved the toolchain itself:

- [Go 1.25](https://go.dev/doc/go1.25) emits DWARF 5 debug information, which reduces link time for large binaries, and sets `GOMAXPROCS` from the container's CPU limit on Linux. That second change matters on Kubernetes runners with CPU limits, where Go previously set `GOMAXPROCS` to the host's core count rather than the CPU the container may use.
- [Go 1.26](https://go.dev/doc/go1.26) enables the Green Tea garbage collector by default, which reduces GC overhead in allocation-heavy programs, including some test suites.
- Go 1.27 is the current release.

Use `go-version-file: go.mod` so CI tracks the version the module declares, and keep the toolchain pinned so the build cache stays valid between runs.

## How 9apes speeds up Go CI

When the cache misses, Go CI is compiling and linking packages, and Go uses every core it has for that. When it hits, the remaining time is restoring the cache and running tests. Both depend on the machine.

9apes runs each GitHub Actions job in a fresh microVM on hardware chosen for high single-core performance with NVMe storage. Fast cores shorten compilation, linking, and single-threaded tests; NVMe speeds up writing and reading a build cache made of many small files. Sizes go up to 8 vCPU on Linux x86_64, so `go test ./...` can build and run more packages at once before you need to shard. See [available runners](/docs/available-runners/).

The [9apes benchmarks](/benchmarks/) include a cold build of [`PeerDB-io/peerdb`](https://github.com/PeerDB-io/peerdb), a project that compiles Go and Rust. On August 27, 2026, the timed build step took a median of 1,283 seconds on GitHub-hosted 2 vCPU runners and 492 seconds on 9apes 2 vCPU runners, across three runs each with dependency caching disabled. That is one project's cold build, not a warm-cache Go pipeline.

To measure your own, follow the [quick start](/docs/quick-start/), keep the cache configuration identical, and change the runner label:

```diff
- runs-on: ubuntu-latest
+ runs-on: 9apes-8vcpu-ubuntu-2404
```

Compare complete job time and [cost](/pricing/) over several runs.

## Does actions/setup-go cache the Go build cache?

Yes. It caches both the module cache and the build cache by default. The key comes from `go.mod` (or `go.sum` if you set `cache-dependency-path`), not your source, so the stored build cache can become stale.

## Why are my Go tests not cached in CI?

Common causes are `-count=1`, a different checkout path without `-trimpath`, different flags or build tags between runs, and tests that read files or environment variables that change on every run.

## Should I use GOCACHEPROG?

Consider it for large repositories where cache archives are big or go stale. It needs a storage backend you operate. For smaller projects, setup-go's built-in cache is enough.

## How do I shard Go tests across GitHub Actions jobs?

Split the output of `go list ./...` across a matrix and run `go test` on each subset. Rebalance using per-package durations from `go test -json` or gotestsum.
