length

Returns the number of UTF-8 bytes in a string, so a multi-byte character such as 'é' counts as more than one.

InputArgumentsReturnsstringno argumentsint[]byteno argumentsintanyno argumentsint
Source
BasicSource

Returns the number of UTF-8 bytes in a string, so a multi-byte character counts as more than one.

Given

{"name": "Ada"}

Template

{{ name | length }}

Result

3
UnicodeSource

Counts UTF-8 bytes rather than runes, so the accented "é" adds two to the total.

Given

{"name": "café"}

Template

{{ name | length }}

Result

5
BasicSource

Returns the number of bytes in a byte-slice value.

Given

{"raw": []byte("hello")}

Template

{{ raw | length }}

Result

5
BasicSource

Counts the elements of a slice, array, or map, the fallback clause reached for a non-string value.

Given

{"items": ["a", "b", "c", "d"]}

Template

{{ items | length }}

Result

4

Counts the entries of a map.

Given

{"headers": {"a": 1, "b": 2}}

Template

{{ headers | length }}

Result

2