Skip to content

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.

Constago v0.1.0 requires Go 1.23 or later.

Terminal window
go install github.com/cohesivestack/constago@v0.1.0

Make sure the Go binary directory is on your PATH, then verify the command:

Terminal window
constago --help

You can also run a pinned version without installing it:

Terminal window
go run github.com/cohesivestack/constago@v0.1.0 --help
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 the command:

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"
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.

Run Constago from the directory containing the configuration file:

Terminal window
constago

The command discovers constago.yaml automatically. To use another path:

Terminal window
constago --config ./config/constago.yaml

For the User struct, the generated file contains:

constago.gen.go
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:

Terminal window
gofmt -w constago.gen.go
go test ./...