/* Wrap long lines on rough column boundaries at spaces Working example: https://play.golang.org/p/3u0X6NyMua Based on algo from RosettaCode, which is nifty https://www.rosettacode.org/wiki/Word_wrap#Go */ package main import ( "fmt" "strings" ) // Wraps text at the specified column lineWidth on word breaks func word_wrap(text string, lineWidth int) string { words := strings.Fields(strings.TrimSpace(text)) if len(words) == 0 { return text } wrapped := words[0] spaceLeft := lineWidth - len(wrapped) for _, word := range words[1:] { if len(word)+1 > spaceLeft { wrapped += "\n" + word spaceLeft = lineWidth - len(word) } else { wrapped += " " + word spaceLeft -= 1 + len(word) } } return wrapped } func main() { longTextStr := ` British voters just shattered political convention in a stunning repudiation of the ruling establishment. Donald Trump is betting America is about to do the same.
", "\n
", "", "-->\n", "