Skip to content
BETAGuardrails are in beta. The library, the configuration format and the CLI command can still change.

Guardrails / golang

Go version is declared

golang/version-declared@v1

The module declares the Go version it is built against, and that version is no older than the configured floor.

Idgolang/version-declared
Versionv1
Categorygolang
Default severitywarning
Interpreterpython3
Timeout30 seconds
Violations tolerated0
Collectsgolang

Collectors

This guardrail gathers nothing itself. It depends on the collectors below, which the CLI runs once per build before any check, and reads what they found out of GUARDRAIL_FACTS. A collector that collects nothing skips this guardrail rather than failing it.

CollectorGathersInputs it is given
golangThe Go build in the project directory: the module it declares, the Go version it asks for, every module of a workspace, the modules it requires directly and indirectly, and the replacements and lock file beside them.projectDir

The inputs above are this guardrail's own, passed straight through. Configuring one in buildnote.json changes what is collected, and two guardrails configured the same way share the one collection.

Configuration

json
{
  "guardrails": {
      "failOn": "error",
      "comment": true,
      "checks": [
          {
              "use": "golang/version-declared@v1",
              "severity": "warning",
              "with": {
                  "projectDir": ".",
                  "minVersion": "1.21"
              },
              "exemptions": []
          }
      ]
  }
}

Inputs

InputDescriptionDefaultEnvironment variable
projectDirDirectory holding the project, relative to the directory the CLI runs in..GUARDRAIL_INPUT_PROJECTDIR
minVersionOldest Go version accepted. A declared version below this is a violation.1.21GUARDRAIL_INPUT_MINVERSION

How to fix

Declare the version the module builds against in go.mod:

go 1.22

go mod edit -go=1.22 writes it. Without the directive the language version is whatever the toolchain defaults to, so a build accepts syntax on one runner that it rejects on another, and a module far below the floor is compiled by a toolchain nobody supports any more.

More in golang

  • golang/checksums-committed. The module commits go.sum, so every module a build downloads is checked against the checksum the commit recorded.
  • golang/no-local-replacements. No replace directive redirects a module to a filesystem path, so the build reads only source the checkout carries.
  • golang/toolchain-pinned. The module names the toolchain that compiles it, rather than leaving the go directive as a floor.

All 4 golang guardrails

Buildnote Limited
Registered in England and Wales, Reg: 16140412