Metago documentation

Templates and metadata

A *.metago file uses Go’s text/template syntax. Every named definition is available to a matching directive.

{{ define "stringer" }}
func ({{ receiver . }} {{ name . }}) String() string {
    return string({{ receiver . }})
}
{{ end }}

Invocation data

FieldMeaning
.PackagePackage metadata.
.MetaCurrent annotation.
.Type, .Method, .FunctionResolved target metadata.
.Name, .TypeName, .KindTarget identity and kind.
.Args, .ArgvNamed and positional arguments.
.Fields, .Methods, .FunctionsSymbols visible to generation.
.Params, .Results, .BodyFunction or method details.
.IsType, .IsMethod, .IsFunctionTarget-kind booleans.
.ValuesConstants discovered for a target type.
.Package.MetasPackage generation annotations in file/line order.

Fields expose .Name, .Type, .Tag, .Embedded, and .Props. Methods expose receiver, parameters, results, body, and props. Interface methods have no receiver or body. Constant .Value is the source expression, not an evaluated number.

Arguments

//mgo:gen templateName positional table=users

Use .Argv or arg 0 for positional arguments. Use .Args, arg "table", or get .Args "table" for named arguments.

{{ default "users" (arg "table") }}

Aggregate a package

.Package.Metas lets one template build registries, route tables, and other artifacts from many annotations:

{{ define "server" }}
func (s Server) Routes() []string {
    return []string{
    {{- range .Package.Metas }}
    {{- if or (eq .Template "get") (eq .Template "post") }}
        "{{ upper .Template }} {{ index .Argv 0 }}",
    {{- end }}
    {{- end }}
    }
}
{{ end }}

Empty templates are valid and can act only as aggregate markers. Property annotations are attached to symbols and do not appear in .Package.Metas.

Imports

The imports helper records required imports and emits an empty string:

{{ imports "strconv" }}
{{ imports "encoding/json" "stdjson" }}

Place it inside the branch that emits code requiring that import.