Generate a file inside each package. Using pre-made templates or custom ones.
go install github.com/ifnotnil/pkgen/cmd/pkgen@latestInside a //go:generate comment line this:
//go:generate pkgen --template '<template>'This will result running only for the current package
Running the pkgen in the project level will result running for all the packages recursively.
pkgen --template '<template>'Or with a custom template
pkgen --template-file /path/to/template.tmpl| Template | File | Description |
|---|---|---|
pkgpath |
pkgpath.tmpl | Simple template that generates the full package path as a string constant |
oteltrace |
oteltrace.tmpl | Basic OpenTelemetry tracing setup with tracer only. It creates a package level tracer, using the full package path as name. |
otel |
otel.tmpl | Full OpenTelemetry setup with tracer, meter, and logger for observability. It creates a package level tracer, meter and logger, using the full package path as name. |
Each template is rendered provided the struct returned from golang.org/x/tools/go/packages
Optionally you can define a config yaml file. By default pkgen will try to read the file .pkgen.yml in the working directory, if exists.
Additionally you can specify a config file for the pkgen to read using the --config <path to yaml> argument.
Example of config file:
templates: # One or more templates can be selected. Pre-configured or custom templates can be selected.
- otel
- template_file: path/to/template.tmpl
packages_query:
patterns: # package patterns that `go list` accepts. Default value is `./...`
- './internal/app' # single package
- './internal/domain/...' # recursive
- './pkg/eventbus'