len
Returns the length of a string, slice, map, or collection.
- Syntax
- len VALUE
- Returns
- int
Usage
The len function returns the length of a string, slice, map, or collection. For a string, the function returns the number of bytes, not the number of characters. Use the strings.RuneCount function to count the characters in a string.
To count the pages in a page collection, you can also use the Len method on a Pages object.
See Go’s text/template documentation for more information.
Examples
{{ "ab" | len }} → 2
{{ "" | len }} → 0With a slice:
{{ slice "a" "b" | len }} → 2
{{ slice | len }} → 0With a map:
{{ dict "a" 1 "b" 2 | len }} → 2
{{ dict | len }} → 0With a page collection:
{{ site.RegularPages | len }} → 42With a string that contains multibyte characters:
{{ "café" | len }} → 5Using the Len method on a Pages object:
{{ site.RegularPages.Len }} → 42Last updated:
November 23, 2026
:
content: Apply style guide to go-template functions (f834b61d1)
Improve this page