2025-09-22 15:35:30 +05:30
# Setup Go Action
2019-06-19 09:44:17 -04:00
2022-12-23 12:03:14 +09:00
[](https://github.com/actions/setup-go/actions/workflows/basic-validation.yml)
2022-04-01 06:29:52 +11:00
[](https://github.com/actions/setup-go/actions/workflows/versions.yml)
2019-08-12 15:13:31 -04:00
2025-09-22 15:00:07 +05:30
This action sets up a Go environment for use in GitHub Actions by:
2025-09-22 15:35:30 +05:30
- Optionally downloading and caching a version of Go by version and adding to PATH
- Registering problem matchers for error output
- Providing intelligent caching for Go modules and build outputs
2019-06-19 09:44:17 -04:00
2025-09-22 15:35:30 +05:30
## Quick Start
2019-07-17 10:50:58 -04:00
2025-09-22 15:35:30 +05:30
```yaml
steps:
- uses: actions/checkout@v5
- uses: actions/setup-go@v6
with:
go-version: '1.21'
- run: go version
```
2025-09-15 15:23:43 -05:00
2025-09-22 15:35:30 +05:30
## Breaking Changes
2025-09-22 15:52:49 +05:30
### V6 - Major Updates
2025-09-19 14:53:21 +05:30
2025-09-22 15:52:49 +05:30
#### Critical Requirements
- **Node Runtime**: Upgraded from Node 20 to Node 24
- **⚠️ Action Required**: Ensure your runner is on version **v2.327.1 or later** for compatibility
- See [Release Notes ](https://github.com/actions/setup-go/releases )
2025-09-19 14:53:21 +05:30
2025-09-22 15:52:49 +05:30
#### Enhanced Toolchain Management
V6 significantly improves toolchain handling for more reliable and consistent Go version selection:
2025-09-19 14:53:21 +05:30
2025-09-22 15:52:49 +05:30
**Now supports `toolchain` directive from go.mod:**
2025-09-22 15:00:07 +05:30
```go
2025-09-19 14:53:21 +05:30
go 1.21.0 // Minimum required version
2025-09-22 15:52:49 +05:30
toolchain go1.21.6 // V6 uses this exact version when specified
2025-09-19 14:53:21 +05:30
```
2025-09-22 15:52:49 +05:30
### V5 - Previous Updates
2024-04-15 10:19:11 -04:00
- Upgraded Node.js runtime from node16 to node20
2025-09-22 15:52:49 +05:30
- See [full release notes ](https://github.com/actions/setup-go/releases )
2020-07-20 19:50:40 +03:00
2025-09-22 15:52:49 +05:30
## Usage
2022-04-20 16:11:14 +02:00
2025-09-22 15:52:49 +05:30
See [action.yml ](action.yml )
2025-09-22 15:35:30 +05:30
2025-09-22 15:52:49 +05:30
### Basic Setup
2022-04-01 06:29:52 +11:00
2020-02-09 14:39:34 -05:00
```yaml
steps:
2025-09-04 03:54:45 +01:00
- uses: actions/checkout@v5
- uses: actions/setup-go@v6
2021-11-08 19:28:08 +09:00
with:
2025-09-22 15:52:49 +05:30
go-version: '1.16.1' # The Go version to download (if necessary) and use
2025-09-22 15:00:07 +05:30
- run: go run hello.go
2020-02-09 14:39:34 -05:00
```
2025-09-22 15:52:49 +05:30
### Version Selection
2025-09-22 15:00:07 +05:30
2025-09-22 15:35:30 +05:30
#### Semantic Versioning
2025-09-22 15:00:07 +05:30
2022-03-29 22:16:03 +03:00
```yaml
2025-09-22 15:52:49 +05:30
# Using caret notation (latest patch release)
2022-03-29 22:16:03 +03:00
steps:
2025-09-04 03:54:45 +01:00
- uses: actions/checkout@v5
- uses: actions/setup-go@v6
2022-03-29 22:16:03 +03:00
with:
2025-09-22 15:52:49 +05:30
go-version: '^1.13.1' # The Go version to download (if necessary) and use
2022-03-29 22:16:03 +03:00
- run: go version
```
2020-02-10 19:18:01 -05:00
```yaml
2025-09-22 15:52:49 +05:30
# Using comparison operators
2020-02-10 19:18:01 -05:00
steps:
2025-09-04 03:54:45 +01:00
- uses: actions/checkout@v5
- uses: actions/setup-go@v6
2022-03-29 22:16:03 +03:00
with:
2025-09-22 15:52:49 +05:30
go-version: '>=1.17.0'
2022-03-29 22:16:03 +03:00
- run: go version
```
2025-09-22 15:52:49 +05:30
> **Note**: Due to YAML parsing behavior, always wrap version numbers in single quotes:
2025-09-22 15:00:07 +05:30
> ```yaml
2025-09-22 15:35:30 +05:30
> go-version: '1.20' # Correct
2025-09-22 15:00:07 +05:30
> ```
2025-09-22 15:52:49 +05:30
> Without quotes, YAML interprets `1.20` as `1.2`, which may cause unexpected behavior.
2025-09-22 15:00:07 +05:30
#### Pre-release Versions
2022-03-29 22:16:03 +03:00
```yaml
2025-09-22 15:52:49 +05:30
# RC version
2022-03-29 22:16:03 +03:00
steps:
2025-09-04 03:54:45 +01:00
- uses: actions/checkout@v5
- uses: actions/setup-go@v6
2021-11-08 19:28:08 +09:00
with:
2025-09-22 15:52:49 +05:30
go-version: '1.18.0-rc.1' # The Go version to download (if necessary) and use
2021-11-08 19:28:08 +09:00
- run: go version
2020-02-10 19:18:01 -05:00
```
2019-07-17 10:50:58 -04:00
```yaml
2025-09-22 15:52:49 +05:30
# Beta version
2019-07-25 21:28:46 -04:00
steps:
2025-09-04 03:54:45 +01:00
- uses: actions/checkout@v5
- uses: actions/setup-go@v6
2022-02-09 14:59:04 +03:00
with:
2025-09-22 15:52:49 +05:30
go-version: '1.16.0-beta.1' # The Go version to download (if necessary) and use
2025-09-22 15:00:07 +05:30
- run: go version
2022-02-09 14:59:04 +03:00
```
2025-09-22 15:52:49 +05:30
### Check Latest Version
2022-02-09 14:59:04 +03:00
2025-09-22 15:52:49 +05:30
The `check-latest` flag defaults to `false` . Use the default or set `check-latest` to `false` if you prefer stability and want to ensure a specific Go version is always used.
2022-02-09 14:59:04 +03:00
2025-09-22 15:52:49 +05:30
If `check-latest` is set to `true` , the action first checks if the cached version is the latest one. If the locally cached version is not the most up-to-date, a Go version will then be downloaded.
2022-02-09 14:59:04 +03:00
2025-09-22 15:52:49 +05:30
> **Note**: Setting `check-latest` to `true` has performance implications as downloading Go versions is slower than using cached versions.
2022-02-09 14:59:04 +03:00
```yaml
steps:
2025-09-04 03:54:45 +01:00
- uses: actions/checkout@v5
- uses: actions/setup-go@v6
2021-11-08 19:28:08 +09:00
with:
2025-09-22 15:52:49 +05:30
go-version: '1.14'
check-latest: true
- run: go run hello.go
2019-07-17 10:50:58 -04:00
```
2022-12-12 10:58:49 +01:00
2025-09-22 15:52:49 +05:30
### Using stable/oldstable Aliases
2025-09-22 15:35:30 +05:30
2025-09-22 15:52:49 +05:30
If `stable` is provided, action will get the latest stable version from the go-versions repository manifest.
2022-12-12 10:58:49 +01:00
2025-09-22 15:52:49 +05:30
If `oldstable` is provided, when current release is 1.19.x, action will resolve version as 1.18.x, where x is the latest patch release.
> **Note**: Using these aliases will result in same version as using corresponding minor release with `check-latest` input set to `true`
2022-12-12 10:58:49 +01:00
```yaml
2025-09-22 15:52:49 +05:30
# Latest stable version
2022-12-12 10:58:49 +01:00
steps:
2025-09-04 03:54:45 +01:00
- uses: actions/checkout@v5
- uses: actions/setup-go@v6
2022-12-12 10:58:49 +01:00
with:
2025-09-22 15:52:49 +05:30
go-version: 'stable'
- run: go run hello.go
2022-12-12 10:58:49 +01:00
```
```yaml
2025-09-22 15:52:49 +05:30
# Previous stable version
2022-12-12 10:58:49 +01:00
steps:
2025-09-04 03:54:45 +01:00
- uses: actions/checkout@v5
- uses: actions/setup-go@v6
2022-12-12 10:58:49 +01:00
with:
2025-09-22 15:52:49 +05:30
go-version: 'oldstable'
2022-12-12 10:58:49 +01:00
- run: go run hello.go
```
2025-09-22 15:52:49 +05:30
### Caching Dependencies and Build Outputs
The action has built-in functionality for caching and restoring go modules and build outputs. It uses `toolkit/cache` under the hood but requires less configuration settings. The cache input is optional, and caching is turned on by default.
The action defaults to search for the dependency file - `go.sum` in the repository root, and uses its hash as a part of the cache key. Use `cache-dependency-path` input for cases when multiple dependency files are used, or they are located in different subdirectories. The input supports glob patterns.
2022-05-25 12:07:29 +02:00
2025-09-22 15:52:49 +05:30
If some problem that prevents success caching happens then the action issues the warning in the log and continues the execution of the pipeline.
#### Caching in Monorepos
2023-03-10 16:25:35 +01:00
2022-05-25 12:07:29 +02:00
```yaml
steps:
2025-09-04 03:54:45 +01:00
- uses: actions/checkout@v5
- uses: actions/setup-go@v6
2022-05-25 12:07:29 +02:00
with:
2025-09-22 15:52:49 +05:30
go-version: '1.17'
check-latest: true
2023-08-25 12:31:19 +02:00
cache-dependency-path: |
2025-09-22 15:00:07 +05:30
subdir/go.sum
tools/go.sum
2025-09-22 15:52:49 +05:30
# Alternative: cache-dependency-path: "**/*.sum"
2022-05-25 12:07:29 +02:00
- run: go run hello.go
2025-09-22 15:00:07 +05:30
```
2024-04-18 22:33:57 +09:00
2025-09-22 15:52:49 +05:30
### Getting Go Version from go.mod File
The `go-version-file` input accepts a path to a `go.mod` file or a `go.work` file that contains the version of Go to be used by a project. The version taken from this file will be:
1. The version from the `toolchain` directive, if there is one, otherwise
2. The version from the `go` directive
The version can specify a patch version or omit it altogether (e.g., `go 1.22.0` or `go 1.22` ).
2022-05-12 17:04:39 +09:00
2025-09-22 15:52:49 +05:30
- If a patch version is specified, that specific patch version will be used
- If no patch version is specified, it will search for the latest available patch version in the cache, versions-manifest.json, and the official Go language website, in that order
> **Note**: If both `go-version` and `go-version-file` inputs are provided then the `go-version` input is used.
The action will search for the `go.mod` file relative to the repository root:
2022-05-12 17:04:39 +09:00
```yaml
steps:
2025-09-04 03:54:45 +01:00
- uses: actions/checkout@v5
- uses: actions/setup-go@v6
2023-03-10 16:25:35 +01:00
with:
2025-09-22 15:52:49 +05:30
go-version-file: 'path/to/go.mod'
- run: go version
2022-05-12 17:04:39 +09:00
```
2019-07-17 10:50:58 -04:00
2025-09-22 15:00:07 +05:30
### Matrix Testing
2022-04-01 06:29:52 +11:00
2019-07-17 10:50:58 -04:00
```yaml
jobs:
2025-09-22 15:52:49 +05:30
build:
2022-01-12 11:40:09 +05:00
runs-on: ubuntu-latest
2019-07-17 10:50:58 -04:00
strategy:
matrix:
2025-09-22 15:52:49 +05:30
go: [ '1.14', '1.13' ]
name: Go ${{ matrix.go }} sample
2019-07-25 21:28:46 -04:00
steps:
2025-09-04 03:54:45 +01:00
- uses: actions/checkout@v5
2025-09-22 15:52:49 +05:30
- name: Setup go
uses: actions/setup-go@v6
2019-07-17 10:50:58 -04:00
with:
2025-09-22 15:52:49 +05:30
go-version: ${{ matrix.go }}
- run: go run hello.go
2019-07-17 10:50:58 -04:00
```
2019-06-19 09:44:17 -04:00
2025-09-22 15:52:49 +05:30
## Version Resolution
2025-09-22 15:00:07 +05:30
2025-09-22 15:52:49 +05:30
The action will first check the local cache for a version match. If a version is not found locally, it will pull it from the main branch of the [go-versions ](https://github.com/actions/go-versions ) repository. On miss or failure, it will fall back to downloading directly from [go dist ](https://go.dev/dl/ ). To change the default behavior, please use the `check-latest` input.
2025-09-22 15:00:07 +05:30
2025-09-22 15:52:49 +05:30
> **Note**: The setup-go action uses executable binaries which are built by Golang side. The action does not build golang from source code.
2022-04-01 06:29:52 +11:00
2025-09-22 15:52:49 +05:30
## Supported Version Syntax
2021-12-01 14:10:32 +03:00
2025-09-22 15:52:49 +05:30
The `go-version` input supports the following syntax:
2022-04-02 00:27:16 +11:00
2025-09-22 15:52:49 +05:30
- **Specific versions**: `1.15` , `1.16.1` , `1.17.0-rc.2` , `1.16.0-beta.1`
- **SemVer's version range syntax**: `^1.13.1` , `>=1.18.0-rc.1`
For more information about semantic versioning, please refer to [semver documentation ](https://semver.org/ ).
2024-11-25 19:37:21 +01:00
2025-09-22 15:52:49 +05:30
## Using setup-go on GHES
2024-11-25 19:37:21 +01:00
2025-09-22 15:52:49 +05:30
`setup-go` comes pre-installed on the appliance with GHES if Actions is enabled. When dynamically downloading Go distributions, `setup-go` downloads distributions from `actions/go-versions` on github.com (outside of the appliance).
2025-09-22 15:00:07 +05:30
2025-09-22 15:52:49 +05:30
These calls to `actions/go-versions` are made via unauthenticated requests, which are limited to 60 requests per hour per IP. If more requests are made within the time frame, then the action leverages the raw API to retrieve the version-manifest. This approach does not impose a rate limit and hence facilitates unrestricted consumption. This is particularly beneficial for GHES runners, which often share the same IP, to avoid the quick exhaustion of the unauthenticated rate limit. If that fails as well the action will try to download versions directly from https://storage.googleapis.com/golang.
2025-09-22 15:00:07 +05:30
2025-09-22 15:52:49 +05:30
If that fails as well you can get a higher rate limit with generating a personal access token on github.com and passing it as the token input to the action:
2022-11-02 12:21:18 +01:00
```yaml
2025-09-22 15:52:49 +05:30
uses: actions/setup-go@v6
with:
token: ${{ secrets.GH_DOTCOM_TOKEN }}
go-version: '1.18'
2022-11-02 12:21:18 +01:00
```
2025-09-22 15:52:49 +05:30
If the runner is not able to access github.com, any Go versions requested during a workflow run must come from the runner's tool cache. See ["Setting up the tool cache on self-hosted runners without internet access" ](https://docs.github.com/en/enterprise-server@latest/admin/github-actions/managing-access-to-actions-from-githubcom/setting-up-the-tool-cache-on-self-hosted-runners-without-internet-access ) for more information.
2025-01-16 08:47:07 -06:00
2025-09-22 15:35:30 +05:30
## Recommended Permissions
2025-01-16 08:47:07 -06:00
2025-09-22 15:52:49 +05:30
When using the setup-go action in your GitHub Actions workflow, it is recommended to set the following permissions to ensure proper functionality:
2025-01-16 08:47:07 -06:00
```yaml
permissions:
2025-09-22 15:52:49 +05:30
contents: read # access to check out code and install dependencies
2025-01-16 08:47:07 -06:00
```
2025-09-22 15:35:30 +05:30
## License
2019-06-19 09:44:17 -04:00
2025-09-22 15:52:49 +05:30
The scripts and documentation in this project are released under the [MIT License ](LICENSE )
2019-06-19 09:44:17 -04:00
2025-09-22 15:35:30 +05:30
## Contributions
2019-06-19 09:44:17 -04:00
2025-09-22 15:52:49 +05:30
Contributions are welcome! See [Contributor's Guide ](docs/contributors.md )
2020-02-09 00:21:39 -05:00
2025-09-22 15:35:30 +05:30
## Code of Conduct
2020-02-09 00:21:39 -05:00
2025-09-22 15:52:49 +05:30
👋 Be nice. See our [code of conduct ](CODE_OF_CONDUCT.md )