{"meta":{"title":"构建和测试 Go","intro":"了解如何创建持续集成 (CI) 工作流来构建和测试 Go 项目。","product":"GitHub Actions","breadcrumbs":[{"href":"/zh/enterprise-cloud@latest/actions","title":"GitHub Actions"},{"href":"/zh/enterprise-cloud@latest/actions/tutorials","title":"教程"},{"href":"/zh/enterprise-cloud@latest/actions/tutorials/build-and-test-code","title":"构建和测试代码"},{"href":"/zh/enterprise-cloud@latest/actions/tutorials/build-and-test-code/go","title":"Go"}],"documentType":"article"},"body":"# 构建和测试 Go\n\n了解如何创建持续集成 (CI) 工作流来构建和测试 Go 项目。\n\n## 简介\n\n本指南介绍如何构建、测试和发布 Go 包。\n\nGitHub 托管的运行器有一个带有预装软件的工具缓存，其中包括 Go 的依赖项。 有关最新软件和预安装版本的 Go 的完整列表，请参阅“[GitHub 托管的运行程序](/zh/enterprise-cloud@latest/actions/concepts/runners/github-hosted-runners#preinstalled-software-for-github-owned-images)”。\n\n## 先决条件\n\n你应该已经熟悉 YAML 语法，以及如何将其与 GitHub Actions 一起使用。 有关详细信息，请参阅“[GitHub Actions 的工作流语法](/zh/enterprise-cloud@latest/actions/reference/workflows-and-actions/workflow-syntax)”。\n\n建议你对 Go 语言有基本的了解。 有关详细信息，请参阅 [Go 入门](https://golang.org/doc/tutorial/getting-started)。\n\n## 使用 Go 工作流模板\n\n若要快速开始使用，请将工作流模板添加到存储库的 `.github/workflows` 目录。\n\nGitHub 提供适用于大多数 Go 项目的 Go 工作流模板。 本指南的后续部分提供了如何自定义此工作流模板的示例。\n\n1. 在 GitHub 上，导航到存储库的主页面。\n\n2. 在仓库名称下，单击“<svg version=\"1.1\" width=\"16\" height=\"16\" viewBox=\"0 0 16 16\" class=\"octicon octicon-play\" aria-label=\"play\" role=\"img\"><path d=\"M8 0a8 8 0 1 1 0 16A8 8 0 0 1 8 0ZM1.5 8a6.5 6.5 0 1 0 13 0 6.5 6.5 0 0 0-13 0Zm4.879-2.773 4.264 2.559a.25.25 0 0 1 0 .428l-4.264 2.559A.25.25 0 0 1 6 10.559V5.442a.25.25 0 0 1 .379-.215Z\"></path></svg> Actions”\\*\\*\\*\\*。\n\n   ![“github/docs”存储库的选项卡的屏幕截图。 “操作”选项卡以橙色边框突出显示。](/assets/images/help/repository/actions-tab-global-nav-update.png)\n\n3. 如果存储库中已有工作流，请单击“新建工作流”。\n\n4. “选择工作流”页面显示一系列推荐的工作流模板。 搜索“go”。\n\n5. 单击“持续集成”\\*\\*\\*\\* 以筛选工作流选择。\n\n6. 在“Go - 按 GitHub Actions”工作流上，单击“配置”\\*\\*\\*\\*。\n\n   ![“选择工作流”页的屏幕截图。 “GO”工作流”上的“配置”按钮以橙色轮廓突出显示。](/assets/images/help/actions/starter-workflow-go.png)\n\n7. 根据需要编辑工作流。 例如更改 Go 版本。\n\n8. 单击“提交更改”。\n\n   工作流 `go.yml` 文件将添加到 `.github/workflows` 存储库的目录中。\n\n## 指定 Go 版本\n\n指定 Go 版本的最简单方法是使用 `setup-go` 提供的 GitHub操作。 有关详细信息，请参阅 [`setup-go` 操作](https://github.com/actions/setup-go/)。\n\n若要在由 GitHub 托管的运行器上使用预安装的 Go 版本，请将相应的版本传递给 `go-version` 操作的 `setup-go` 属性。 此操作从每个运行器上的工具缓存中查找特定版本的 Go，并将必要的二进制文件添加到 `PATH`。 这些更改将在任务的剩余时间内持续有效。\n\n`setup-go` 操作是将 Go 与 GitHub Actions 配合使用的推荐方式，因为它有助于确保在不同的运行器和不同版本的 Go 中表现一致。 如果使用自托管运行器，则必须安装 Go 并将其添加到 `PATH`。\n\n### 使用多个版本的 Go 程序语言\n\n```yaml copy\nname: Go\n\non: [push]\n\njobs:\n  build:\n\n    runs-on: ubuntu-latest\n    strategy:\n      matrix:\n        go-version: [ '1.19', '1.20', '1.21.x' ]\n\n    steps:\n      - uses: actions/checkout@v6\n      - name: Setup Go ${{ matrix.go-version }}\n        uses: actions/setup-go@v5\n        with:\n          go-version: ${{ matrix.go-version }}\n      # You can test your matrix by printing the current Go version\n      - name: Display Go version\n        run: go version\n```\n\n### 使用特定的 Go 版本\n\n可以将作业配置为使用 Go 的特定版本，例如 `1.20.8`。 或者，您也可以使用语义版本语法来获得最新的次要版本。 此示例使用了 Go 1.21 的最新修补版本。\n\n```yaml copy\n      - name: Setup Go 1.21.x\n        uses: actions/setup-go@v5\n        with:\n          # Semantic version range syntax or exact version of Go\n          go-version: '1.21.x'\n```\n\n## 安装依赖\n\n可以使用 `go get` 安装依赖项：\n\n```yaml copy\n    steps:\n      - uses: actions/checkout@v6\n      - name: Setup Go\n        uses: actions/setup-go@v5\n        with:\n          go-version: '1.21.x'\n      - name: Install dependencies\n        run: |\n          go get .\n          go get example.com/octo-examplemodule\n          go get example.com/octo-examplemodule@v1.3.4\n```\n\n### 缓存依赖项\n\n可以使用 [`setup-go` 操作](https://github.com/actions/setup-go)缓存和还原依赖项。 使用 `setup-go` 操作时，默认启用缓存。\n\n`setup-go` 操作将在仓库根目录中搜索依赖项文件 `go.sum`，并使用该依赖项文件的哈希作为缓存密钥的一部分。\n\n当使用多个依赖项文件或这些文件位于不同的子目录中时，可以使用 `cache-dependency-path` 参数。\n\n```yaml copy\n      - name: Setup Go\n        uses: actions/setup-go@v5\n        with:\n          go-version: '1.17'\n          cache-dependency-path: subdir/go.sum\n```\n\n如果有自定义要求或需要更精细的缓存控制，可以使用 [`cache` 操作](https://github.com/marketplace/actions/cache)。 有关详细信息，请参阅“[依赖项缓存参考](/zh/enterprise-cloud@latest/actions/reference/workflows-and-actions/dependency-caching)”。\n\n## 构建和测试代码\n\n您可以使用与本地相同的命令来构建和测试代码。 此示例工作流演示如何在作业中使用 `go build` 和 `go test`：\n\n```yaml copy\nname: Go\non: [push]\n\njobs:\n  build:\n    runs-on: ubuntu-latest\n\n    steps:\n      - uses: actions/checkout@v6\n      - name: Setup Go\n        uses: actions/setup-go@v5\n        with:\n          go-version: '1.21.x'\n      - name: Install dependencies\n        run: go get .\n      - name: Build\n        run: go build -v ./...\n      - name: Test with the Go CLI\n        run: go test\n```\n\n## 将工作流数据打包为构件\n\n工作流程完成后，您可以上传产生的项目进行分析。 例如，您可能需要保存日志文件、核心转储、测试结果或屏幕截图。 以下示例演示如何使用 `upload-artifact` 操作上传测试结果。\n\n有关详细信息，请参阅“[使用工作流工件存储和共享数据](/zh/enterprise-cloud@latest/actions/tutorials/store-and-share-data)”。\n\n```yaml copy\nname: Upload Go test results\n\non: [push]\n\njobs:\n  build:\n\n    runs-on: ubuntu-latest\n    strategy:\n      matrix:\n        go-version: [ '1.19', '1.20', '1.21.x' ]\n\n    steps:\n      - uses: actions/checkout@v6\n      - name: Setup Go\n        uses: actions/setup-go@v5\n        with:\n          go-version: ${{ matrix.go-version }}\n      - name: Install dependencies\n        run: go get .\n      - name: Test with Go\n        run: go test -json > TestResults-${{ matrix.go-version }}.json\n      - name: Upload Go test results\n        uses: actions/upload-artifact@v4\n        with:\n          name: Go-results-${{ matrix.go-version }}\n          path: TestResults-${{ matrix.go-version }}.json\n```"}