Write templates that write Go

Now in Beta

01 · Write a Metago template

{{/* stringer.metago */}}
{{ define "stringer" }}
func (v {{ name . }}) String() string {
    switch v {
    {{- range .Values }}
    case {{ .Name }}:
        return {{ quote .Name }}
    {{- end }}
    default:
        return "unknown"
    }
}
{{ end }}

02 · Add the directive to your type

// status.go
package example

//mgo:gen stringer
type Status int

const (
    StatusPending Status = iota
    StatusRunning
    StatusDone
)

03 · Run Metago to generate Go

// meta.go
// Code generated by metago; DO NOT EDIT.

package example

func (v Status) String() string {
    switch v {
    case StatusPending:
        return "StatusPending"
    case StatusRunning:
        return "StatusRunning"
    case StatusDone:
        return "StatusDone"
    default:
        return "unknown"
    }
}

Get started

Install Metago with Go:

go install github.com/guillemus/metago@latest

Ready-to-use binaries are also available on GitHub Releases.

With Go 1.24 or later, pin Metago as a project tool and call it from a single go:generate directive:

go get -tool github.com/guillemus/metago@latest
//go:generate go tool metago .

Then run go generate ./.... Metago scans the supplied root recursively.

Run an installed copy directly from the root of your project with:

metago

Optionally, install the Metago skill to help your coding agent start generating templates:

npx skills add guillemus/metago --skill metago