Indenting a text document
The previous recipe depicts how to do string padding and whitespace trimming. This one will guide you through the indentation and unindentation of a text document. Similar principles from the previous recipes will be used.
How to do it...
- Open the console and create the folder
chapter02/recipe12
. - Create the file
main.go
with the following content:
package main import ( "fmt" "strconv" "strings" "unicode" ) func main() { text := "Hi! Go is awesome." text = Indent(text, 6) fmt.Println(text) text = Unindent(text, 3) fmt.Println(text) text = Unindent(text, 10) fmt.Println(text) text = IndentByRune(text, 10, '.') fmt.Println(text) } // Indent indenting the input by given indent and rune func IndentByRune(input string, indent int, r rune) string { return...