Getting started

1. Add the dependency

For the engine plus the Sprig functions:


    org.alexmond
    gotmpl4j-sprig
    0.1.0

The Sprig module depends on gotmpl4j-core transitively and registers its functions automatically through ServiceLoader. If you want the engine with Go built-ins only, depend on gotmpl4j-core instead.

2. Render a template

import org.alexmond.gotmpl4j.GoTemplate;
import java.io.StringWriter;
import java.util.Map;

var tpl = new GoTemplate();                 // auto-loads Sprig from the classpath
tpl.parse("greet", "Hello {{ .name | upper }}!");

var out = new StringWriter();
tpl.execute("greet", Map.of("name", "world"), out);
//  -> "Hello WORLD!"

render is a convenience that returns the string directly:

String s = new GoTemplate()
        .parse("greet", "Hello {{ .name | title }}!")
        .render("greet", Map.of("name", "world"));   // "Hello World!"

3. Data binding

The "dot" (.) is the current data value. Fields resolve against Map keys, JavaBean getters, and public fields:

record User(String name, int age) {}

new GoTemplate()
        .parse("u", "{{ .name }} is {{ .age }}")
        .render("u", new User("Ada", 36));   // "Ada is 36"

4. Controlling function loading

By default new GoTemplate() discovers every FunctionProvider on the classpath. Use the builder for finer control:

GoTemplate tpl = GoTemplate.builder()
        .noAutoDiscovery()                 // skip ServiceLoader discovery
        .htmlEscaping()                    // enable contextual HTML auto-escaping
        .withFunctions(Map.of("greet", args -> "hi " + args[0]))
        .build();

5. Reusing a parsed template

Parsing is the expensive step; execution is cheap. Parse once and execute many times — a parsed GoTemplate (without HTML escaping) is safe to execute concurrently. For a pre-resolved handle to one named template, use compiled:

var compiled = tpl.parse("row", "{{ .id }},{{ .name }}\n").compiled("row");
for (var record : records) {
    compiled.execute(record, writer);
}