Check the documentation about the built-in functions of text/template
:
len
can produce an integer from a string: {{ len "abc" }}
printf
can produce a string of a given length from an integer: {{ printf "%*s" 3 "" }}
slice
can truncate a string to a given length from an integer: {{ slice "abc" 0 2 }}
slice
can truncate a string by a given length from an integer: {{ slice "abc" 1 }}
You can combine both to increment an integer:
{{ len (printf "a%*s" 3 "") }}
will produce:
4
Or to decrement an integer:
{{ len (slice (printf "%*s" 3 "") 1) }}
shows:
2
You can also define templates to reuse pieces of templates.
So we can define 1 argument functions with templates (see on the Go Playground):
{{ define "inc" }}{{ len (printf "%*s " . "") }}{{ end -}}
{{ define "op" }}{{.}} + 1 = {{ template "inc" . }}{{ end -}}
{{ template "op" 2 }}
{{ template "op" 5 }}
shows:
2 + 1 = 3
5 + 1 = 6
We can go further if the input of the template is an array of integers ([]int{4, 2, 7}
):
{{ define "inc" }}{{ len (printf "%*s " . "") }}{{ end -}}
{{ define "op" }}{{.}} + 1 = {{ template "inc" . }}{{ end -}}
{{- range . -}}
{{ template "op" . }}
{{ end -}}
See full example on the Go Playground.