Go Snippets Tooling
This directory contains the scripts and configuration for building, running, and testing the Go snippets located in examples/go/.
Overview
The tooling is designed to ensure that all Go snippets are continuously validated and to provide a fast feedback loop for developers. It consists of a unified runner script, a configuration file to manage the list of snippets, and a suite of unit tests for the runner itself.
Key Components
runner.sh: The main script for building and running Go snippets.files_to_test.txt: The configuration file that lists all Go snippets to be tested.check_go_snippets.sh: A PR check script that ensures all.gofiles are registered infiles_to_test.txt.runner_test.sh: Unit tests for therunner.shscript.
Understanding the Test Runner
The runner.sh script is the core of this tooling. It is crucial to understand how it identifies which snippets to build, especially when making changes.
- Strict Adherence to
files_to_test.txt: The runner strictly relies on thefiles_to_test.txtconfiguration file. When a file is changed in a Pull Request, the runner looks for that file's path (relative toexamples/go/) as a substring within any line offiles_to_test.txt. If the file is not found (e.g., you added a new file but forgot to register it, or it is an ignored file type), the runner will not trigger a build for that specific change. - Multi-file
package main: If a snippet is apackage main(an executable application) that is split across multiple.gosource files in the same directory (e.g.,main.goandhelper.go), all of these files must be listed on the same line infiles_to_test.txt.- Correct:
snippets/mytask/main.go snippets/mytask/helper.go - Incorrect: Listing them on separate lines or omitting the helper. This will cause the
go buildcommand to fail with "undefined symbol" errors because the compiler won't see the helper file.
- Correct:
- Test Files (
_test.go): Files ending in_test.goare explicitly excluded fromfiles_to_test.txtand are ignored by the runner. Consequently, changing a_test.gofile will not automatically trigger a build of the main snippet in that directory. Thego buildcommand used by the runner ignores test files.
How to Use
Automatic Execution (CI/CD)
The scripts are primarily designed to be run automatically by GitHub Actions.
-
On Pull Requests: When a pull request is opened, two workflows are triggered:
- Go Snippets Build on PR and Schedule: This workflow runs
check_go_snippets.shto ensure new files are registered. It then intelligently builds only the.gofiles that were changed in the PR. - Go Build and Test on PR: This workflow runs a full build of all Go snippets and executes any unit tests (
go test ./...) to ensure that a change has not broken any other part of the Go codebase.
- Go Snippets Build on PR and Schedule: This workflow runs
-
Scheduled Runs: A full regression build of all Go snippets is run automatically every Sunday at 3:00 AM UTC to catch any potential issues.
Manual Execution
You can also run the scripts locally to test your changes before pushing. All commands should be run from the root of the repository.
Building All Snippets
To run a full build of every Go snippet listed in files_to_test.txt:
./tools/go-snippets/runner.sh build
Building Specific Snippets
To build one or more specific Go snippets (for example, if you are working on them and want a quick check):
./tools/go-snippets/runner.sh build examples/go/snippets/quickstart/main.go
Running the Unit Tests
To run the unit tests for the runner.sh script itself:
./tools/go-snippets/runner_test.sh
Maintaining the Snippet List
Adding a New Snippet
-
Create your new
.gofile (e.g.,examples/go/snippets/my-new-snippet/main.go). -
Open
tools/go-snippets/files_to_test.txt. -
Add a new line with the path to your file, relative to the
examples/go/directory.# In files_to_test.txt snippets/my-new-snippet/main.go -
If your snippet is part of a package that requires multiple files to be built together, add them all to the same line:
# In files_to_test.txt snippets/my-multi-file-snippet/main.go snippets/my-multi-file-snippet/helpers.go
The check_go_snippets.sh script will automatically run on your PR and remind you if you've forgotten to add your new file to the list.