Skip to content

go:generate Workflow

Go’s generation convention makes the command discoverable next to the types it serves.

Place a directive in any non-generated file in the package or module where you want developers to start generation:

generate.go
package model
//go:generate go run github.com/cohesivestack/constago@v0.1.0 --config ../constago.yaml

Paths are resolved from the package directory where go generate runs. Set input.dir in the YAML accordingly.

For a repository-wide configuration stored at the module root:

constago.yaml
input:
dir: "."
include: ["**/*.go"]
exclude:
- "**/*_test.go"
- "**/constago.gen.go"
output:
file_name: "constago.gen.go"
elements:
- name: "json"
input:
mode: "tag"
tag_priority: ["json"]
output:
mode: "constant"

Run from the module root:

Terminal window
go generate ./...
gofmt -w .
go test ./...

Commit generated files, regenerate them in CI, and fail if the working tree changes:

Terminal window
go generate ./...
gofmt -w .
go test ./...
git diff --exit-code

Pinning @v0.1.0 makes local and CI output use the same generator release.

Constago does not automatically ignore output.file_name. Exclude the current generated filename and any older generated filenames:

input:
exclude:
- "**/*_test.go"
- "**/constago.gen.go"
- "**/legacy_fields.gen.go"