Getting Started
Constago scans Go structs and generates type-safe names for their fields and tags. It can produce package constants, grouped accessor values, and methods that return metadata or the field’s runtime value.
Requirements
Section titled “Requirements”Constago v0.1.0 requires Go 1.23 or later.
Install the command
Section titled “Install the command”go install github.com/cohesivestack/constago@v0.1.0Make sure the Go binary directory is on your PATH, then verify the command:
constago --helpYou can also run a pinned version without installing it:
go run github.com/cohesivestack/constago@v0.1.0 --helpCreate a struct
Section titled “Create a struct”package model
type User struct { Name string `json:"name" title:"Name"` Country string `json:"country" title:"Country"`}Configure Constago
Section titled “Configure Constago”Create constago.yaml in the directory where you will run the command:
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"Keep the generated filename in input.exclude. Otherwise, a later run can scan
the previous generated file because the default **/*.go include matches it.
Generate code
Section titled “Generate code”Run Constago from the directory containing the configuration file:
constagoThe command discovers constago.yaml automatically. To use another path:
constago --config ./config/constago.yamlFor the User struct, the generated file contains:
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}Format the generated file and compile the package:
gofmt -w constago.gen.gogo test ./...Choose what to generate next
Section titled “Choose what to generate next”- Read Configuration for every key and default.
- Use Select Files & Types to control scanning.
- See Generate Elements for constants, grouped accessors, and transforms.
- See Generate Getters for metadata and runtime-value methods.
- Import Constago directly with the Library API.