last

Returns the last element of a slice, or the trailing byte of a string.

An empty string is a miss the default modifier can catch.

InputArgumentsReturnsstringno argumentsstring[]byteno argumentsstring[]anyno argumentsany
Source

Behaviors

Catchable miss
When the data it looks for is not there, it reports a miss rather than failing the render, so default can supply a fallback and an if or for reads the absence as false.
BasicSource

Returns the trailing byte of a string as a one-byte string.

Given

{"word": "hello"}

Template

{{ word | last }}

Result

o
BasicSource

Reads the trailing byte of a []byte buffer as text, so a file read or an HTTP body behaves the same as a string rather than yielding a raw byte number from the slice clause.

Given

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

Template

{{ buffer | last }}

Result

o
BasicSource

Returns the last element of a slice as-is.

Given

{"items": ["espresso", "latte", "macchiato"]}

Template

{{ items | last }}

Result

macchiato
NumbersSource

Returns the last element of a slice of numbers, keeping it a number.

Given

{"scores": [3, 1, 4, 1, 5]}

Template

{{ scores | last }}

Result

5
EmptySource

Shows that last has nothing to return for an empty string, so it reports an error.

Given

{"word": ""}

Template

{{ word | last }}

Result

error: failed to render template: failed to render variable token 'word': modifier "last": function failed to apply: last expected a non-empty string