Skip to content

Instantly share code, notes, and snippets.

@henrikh
Created October 22, 2011 18:32
Show Gist options
  • Save henrikh/1306333 to your computer and use it in GitHub Desktop.
Save henrikh/1306333 to your computer and use it in GitHub Desktop.
Very simple line-breaker
package main
import (
"fmt"
"strings"
)
var text = "I am a text that needs to be formatted. Please get started, I really can't wait to have so much fun with this. I also have some text that is supposed to break everything, but that won't really happen, 'cus I'm a badass programmer."
const WIDTH = 50
func main(){
slicedText := strings.Split(text, " ")
widthLeft := WIDTH
text := ""
for i := range slicedText {
currentWord := slicedText[i]
if len(currentWord) + 1 >= widthLeft {
widthLeft = WIDTH
text += "\n"
}
text += slicedText[i] + " "
widthLeft -= len(currentWord) + 1
}
fmt.Println(text)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment