Check is a Go library for statically type-checking text/template
and html/template
. It helps catch template/type mismatches early, making refactoring safer when changing types or templates.
To use it, call Execute
and provide:
- a
types.Type
for the template’s data (.
), and - the template’s
parse.Tree
.
See example_test.go for a working example.
Originally built as part of muxt
, this package also powers the muxt check
CLI command. If you only need command-line checks, use muxt check
directly.
Unlike muxt
, which requires templates to be defined as global variables, this package lets you map templates to data parameters more flexibly (at the cost of some verbosity).
For a more robust and easier-to-configure alternative, consider jba/templatecheck.
-
Global
Holds type and template resolution state. Constructed withNewGlobal
. -
Execute
Entry point to validate a template tree against a giventypes.Type
. -
TreeFinder
/FindTreeFunc
Resolves other templates by name (wrappingTemplate.Lookup
). -
Functions
A set of callable template functions. ImplementsCallChecker
.- Use
DefaultFunctions(pkg *types.Package)
to get the standard built-ins. - Extend with
Functions.Add
.
- Use
-
CallChecker
Interface for validating function calls within templates.
-
Type required You must provide a
types.Type
that represents the template’s root context (.
). -
Function sets Currently, default functions do not differentiate between
text/template
andhtml/template
. -
Third-party template packages Compatibility with specialized template libraries (e.g. safehtml) has not been fully tested.
-
Runtime-only errors
Execute
checks static type consistency but cannot detect runtime conditions such as out-of-range indexes. The standard library will try to dereference boxed types that may contain any type. Errors introduced by changes on a boxed type can not be caught by this package.