Skip to main content
The view engine provides built-in template functions that make your templates more expressive. This guide covers all available functions and how to add your own.

Built-in Functions

These functions are available in all templates.

Data Structure Functions

dict

Creates a map from key-value pairs:

list

Creates a slice from values:

String Functions

upper

Converts a string to uppercase:

lower

Converts a string to lowercase:

trim

Removes leading and trailing whitespace:

contains

Checks if a string contains a substring:

replace

Replaces occurrences of a substring:
The third argument is the maximum number of replacements (-1 means all).

split

Splits a string into a slice:

join

Joins a slice into a string:

hasPrefix

Checks if a string starts with a prefix:

hasSuffix

Checks if a string ends with a suffix:

Summary Table

Go’s Standard Functions

Go’s html/template package provides these built-in functions:

Comparison Functions

Logic Functions

Other Standard Functions

Adding Custom Functions

Add your own functions via the Funcs option:

Using Custom Functions

Common Custom Functions

Date Formatting

Number Formatting

URL Helpers

Safe HTML (Use Carefully!)

Default Values

Best Practices

1. Keep Functions Pure

Functions should not have side effects:

2. Handle Edge Cases

3. Use Pipelines for Readability

4. Document Your Functions

5. Test Custom Functions

6. Be Careful with safeHTML

Only use safeHTML with content you completely trust:

Complete Example