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.Typefor 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.
- 
GlobalHolds type and template resolution state. Constructed withNewGlobal. - 
ExecuteEntry point to validate a template tree against a giventypes.Type. - 
TreeFinder/FindTreeFuncResolves other templates by name (wrappingTemplate.Lookup). - 
FunctionsA set of callable template functions. ImplementsCallChecker.- Use 
DefaultFunctions(pkg *types.Package)to get the standard built-ins. - Extend with 
Functions.Add. 
 - Use 
 - 
CallCheckerInterface for validating function calls within templates. 
- 
Type required You must provide a
types.Typethat represents the template’s root context (.). - 
Function sets Currently, default functions do not differentiate between
text/templateandhtml/template. - 
Third-party template packages Compatibility with specialized template libraries (e.g. safehtml) has not been fully tested.
 - 
Runtime-only errors
Executechecks 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.