How to truncate a string in a Golang template

You can use printf in templates, which acts as fmt.Sprintf. In your case truncating a string would be as easy as:

{{ printf "%.25s" .Content }}

You can also pass the number as a separate integer argument to printf:

{{ printf "%.*s" 25 .Content }}

Note from the documentation:

Width and precision are measured in units of Unicode code points, that is, runes. (This differs from C’s printf where the units are always measured in bytes.)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)