Home › Go

go: errors parsing go.mod: unknown directive: toolchain — how to fix

go: errors parsing go.mod:
/home/me/project/go.mod:5: unknown directive: toolchain

Quick fix: Check go version. Upgrade to Go 1.21+ (in CI, use actions/setup-go with go-version-file: go.mod; in Docker, use a newer golang: image tag). If you must support older Go, delete the toolchain go1.x.y line from go.mod (and make the go line a two-part version like go 1.20).

What it means

Go 1.21 added a toolchain directive to go.mod:

module example.com/app

go 1.21

toolchain go1.22.3

It records the Go toolchain the module prefers. Go versions before 1.21 don't know this keyword and refuse to parse the file. So the error always means an older go binary is reading a go.mod written by a newer one.

Newer Go commands add the line by themselves, for example when go get or go mod tidy raises the go version, which is why it can show up without anyone editing go.mod by hand.

go version          # what is actually running
which -a go         # is an old copy earlier in PATH?

Install Go 1.21 or newer from go.dev/dl or your package manager. From 1.21 on, Go can also download the toolchain named in go.mod automatically (controlled by the GOTOOLCHAIN setting), so a 1.21+ binary handles newer modules on its own.

If you upgraded but still see the error, an older go is earlier in your PATH, for example a distro package in /usr/bin next to /usr/local/go/bin. Fix PATH or remove the old one.

Fix 2: CI and Docker images

GitHub Actions: take the version from go.mod instead of hard-coding an old one:

- uses: actions/setup-go@v5
  with:
    go-version-file: go.mod

Docker: bump the base image:

FROM golang:1.22 AS build

Also check other tools that bundle their own Go or read go.mod, such as linters and IDE plugins, and update them.

Fix 3: keep the module usable on older Go

If you maintain a library that should still build with Go 1.20 or older, remove the directive and keep the go line in the old two-part form:

module example.com/lib

go 1.20

You can delete the line by hand, or with a 1.21+ toolchain run:

go mod edit -toolchain=none

Older Go also rejects three-part versions like go 1.21.0 in the go line (see invalid go version: must match format), so change both lines.

Your CI should then test on the oldest Go you claim to support, because a newer go mod tidy will add the line again whenever it raises the go version.

Last updated 2026-12-04.