/* 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.

Voters in the UK did more than reject the European Union and topple their pro-EU Prime Minister David Cameron in a referendum Thursday.
They also set off a cascade of events that could spark global economic chaos, remake the Western world, reverberate through November's presidential election and challenge U.S. security for years to come.
` fmt.Printf("Original: [%v] \n", longTextStr) fmt.Println("--------------------------------") wrapped := word_wrap(longTextStr, 70) // Some minimal html fixups // Note: this can introduce newlines inside class attributes, but that's perfectly // valid html (nb: http://stackoverflow.com/a/14928606) r := strings.NewReplacer("

", "\n

", "", "-->\n", "