Generate compiler-visible Go code from struct field names and tags.
Warning
Constago is in early development and is not yet recommended for production use. Review generated code before committing it.
Constago is a build-time code generator. It scans selected Go structs and turns
field names and tags such as json, xml, yaml, or your own tags into:
- package constants, such as
JsonUserName; - grouped field accessors, such as
TitleUser.Name; and - methods that return field metadata, the field's runtime value, or both.
This is useful anywhere your application needs stable field identifiers—for
example, validation errors, forms, filters, API payloads, or database mappings.
Instead of repeating string literals such as "display_name" throughout the
codebase, you can use generated Go identifiers that are discoverable by your
editor and checked by the compiler.
Constago does not serialize data, validate values, or act as an ORM. It derives metadata from your structs and generates ordinary Go source code; the generated API does not require runtime reflection.
Constago requires Go 1.23 or later.
Install the command:
go install github.com/cohesivestack/constago@latestGiven this model:
// user.go
package model
type User struct {
Name string `json:"name" title:"Name"`
Country string `json:"country" title:"Country"`
}Create constago.yaml in the directory where you will run Constago:
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"
format:
prefix: "Json"
- name: "title"
input:
mode: "tag"
tag_priority: ["title"]
output:
mode: "struct"
format:
prefix: "Title"
getters:
- name: "metadata"
returns: ["json", "title", ":value"]
output:
prefix: "Metadata"
format: "pascal"Run:
constagoConstago writes constago.gen.go in the selected package. The generated code
includes:
const (
JsonUserName = "name"
JsonUserCountry = "country"
)
var TitleUser = struct {
Name string
Country string
}{
Name: "Name",
Country: "Country",
}
func (_struct *User) MetadataName() (string, string, string) {
return "name", "Name", _struct.Name
}
func (_struct *User) MetadataCountry() (string, string, string) {
return "country", "Country", _struct.Country
}You can then refer to metadata as JsonUserName or TitleUser.Name, and call
user.MetadataName() when you need the configured metadata together with the
current field value.
An element defines one string value for every selected field.
input.mode: tagreads the first available tag intag_priority.input.mode: fieldderives the value from the Go field name.input.mode: tagThenFieldreads tags first and falls back to the field name.output.mode: constantemits one package constant per field.output.mode: structemits a grouped package variable.output.mode: noneemits no standalone value but keeps the element available to generated getters.
Generated identifiers support camel, pascal, snake, and snakeUpper
formats, optional prefixes and suffixes, and configurable value transformations.
A getter generates one method per selected field. Its returns list can contain
configured element names and the special :value token:
getters:
- name: "key"
returns: ["json", ":value"]The metadata values are strings. :value returns the actual field value using
its declared Go type, including types that require imports.
You can select source files with include/exclude globs or package:NAME, filter
struct and field names with the only and except regular-expression options,
include unexported declarations, or opt individual declarations in and out:
//constago:include
type User struct {
Name string `json:"name"`
Password string `json:"password" constago:"exclude"`
}See Select Files & Types for the complete selection rules.
Without --config, the command looks for constago.yaml in the current
directory:
constago
constago --config ./config/constago.yaml
constago --helpBasic input and output settings can also be supplied through command-line flags
or CONSTAGO_* environment variables. Elements and getters are configured in
YAML.
For all keys, defaults, and valid values, see the configuration reference. The documentation also covers the CLI, elements, getters, and library API.
- Constago creates the configured output filename in each selected package directory.
- Add that filename to
input.exclude; generated files are not excluded automatically. - Struct tag options after the first comma are removed. For example,
json:"name,omitempty"becomesname. - Tag values are otherwise literal:
json:"-"generates-unless you exclude that field. - Format the output and compile your packages after generation:
gofmt -w constago.gen.go
go test ./...For reproducible generation in a project, see the
go generate guide.
Run the test suite:
go test ./...Copyright © 2026 Carlos Forero
Constago is developed and maintained by Cohesive Stack LLC and released under the MIT License.