Template Variables¶
Variable Syntax¶
Templates use dunder-style placeholders: __VarName__. These placeholders are replaced during scaffolding in file contents and path names.
Default Variables¶
gohatch automatically sets these variables based on the module path:
| Variable | Default Value |
|---|---|
ProjectName |
Output directory name |
GitUser |
Second path element of module (e.g., github.com/user/repo → user) |
Both can be overridden with --var.
Setting Variables¶
Use the -v or --var flag to set variables:
Multiple variables:
Example¶
In your template files:
After scaffolding with --var Author="Oliver":
Path Renaming¶
Variables can also be used in directory and file names:
Template structure: After scaffolding:
cmd/__ProjectName__/main.go → cmd/myapp/main.go
__ProjectName___test.go → myapp_test.go
This allows templates where the directory structure adapts to the project name.
Extension Matching¶
By default, variable substitution and module path rewriting apply to .go files and go.mod. Use -e to include additional file types.
Each -e pattern is treated as both a potential filename and extension:
-e ymlmatches files namedymland files with.ymlextension-e justfilematches files namedjustfileand files with.justfileextension
Templates can also specify default extensions in .gohatch.toml (see Creating Templates). CLI extensions are merged with config extensions.
Interactive Prompting¶
When gohatch detects unset variables in your template, it prompts you interactively to provide values — if running in a terminal.
- Variables used in paths are marked as required (you must provide a value)
- Variables used only in file contents are optional (press Enter to skip)
Prompting is skipped when:
- The
--no-promptflag is set - stdin is not a terminal (e.g., in CI pipelines)
Unset Variable Behavior¶
gohatch scans the template for variable placeholders that have no value assigned:
- In paths: Always an error. You must set the variable with
--varor provide it when prompted. - In file contents (default): A warning is shown and the placeholder is removed (replaced with empty string).
- In file contents (
--strict): Treated as an error. Scaffolding is aborted.
Binary File Detection¶
gohatch automatically detects binary files and skips them during variable substitution and module path rewriting. This prevents corruption of images, compiled assets, and other non-text files in your template.