Skip to content
Node: _.truncate(s, n)github.com/sahilkhaire/gox/str

Truncate

Overview

Truncate shortens s to max runes, adding suffix if needed.

If you are coming from Node.js, the closest pattern is _.truncate(s, n).

Signature

go
func Truncate(s string, max int, suffix string) string

Compare: Node.js · Standard Go · gox

js
_.truncate(str, { length: 10 });
go
if len([]rune(s)) > 10 { s = string([]rune(s)[:10]) + "…" }
go
import "github.com/sahilkhaire/gox/str"

short := str.Truncate(long, 10)

Example

go
import "github.com/sahilkhaire/gox/str"

short := str.Truncate(long, 10)

Tips

Truncates by rune count, not byte length — safe for Unicode.

Standard library alternative

Use the standard library directly:

go
if len([]rune(s)) > 10 { s = string([]rune(s)[:10]) + "…" }

Back to str package overview

MIT Licensed · Built for Node.js developers moving to Go