Metago documentation

Template helpers

Metago includes standard Go template functions plus helpers for metadata, names, tags, properties, types, imports, and collections.

Metadata

HelperResult
name .Name of an invocation, type, field, method, or value.
typeof .Underlying or field type.
keys .Field names, or sorted map keys.
fieldNames .Target field names.
methodNames .Comma-separated method names.

Struct tags

For ID int \json:“id,omitempty”``:

HelperResult
tag . "json"id,omitempty
tagName . "json"id
tagOpts . "json"Options after the first comma.
tagHas . "json" "omitempty"Whether the option exists.
tagExists . "json"Whether the tag key exists.

Properties

HelperResult
prop . "validate" "max"A named property value, or empty string.
props . "validate"The complete group with .Args and .Argv.
propHas . "validate" "required"Whether a bare flag exists.
propExists . "pii"Whether the namespace exists.

These accept fields, types, methods, functions, and invocations.

Field filters

fieldsWithTag, fieldsWithoutTag, exportedFields, unexportedFields, embeddedFields, and nonEmbeddedFields accept an invocation, .Type, or a field slice.

{{ range exportedFields (fieldsWithTag . "json") }}
    {{ name . }}
{{ end }}

Naming

HelperExample
snake .NameUserIDuser_id
kebab .NameUserIDuser-id
camel .Nameuser_iduserID
pascal .Nameuser_idUserID
initial .NameUseru
receiver .UserProfileup
exported .NameTests exported visibility.
unexported .NameLowercases the first rune.

Strings

lower, upper, contains, hasPrefix, hasSuffix, trimPrefix, trimSuffix, replace, split, join, and quote provide common string operations. quote emits a safe Go string literal.

Types

Use isString, isInt, isBool, isFloat, isSlice, isMap, and isPointer for branches. elem returns the element of []T or *T; zero returns a Go zero value.

{{ if isPointer . }}
if value == nil { return {{ zero (elem .) }} }
{{ end }}

Data

HelperPurpose
dict "k" vCreate a map.
list "a" "b"Create a list.
get m "key"Read a map key or exported struct field.
arg 0, arg "key"Read directive arguments.
default fallback valueReplace a zero value.