O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. Open the console and create the folder chapter02/recipe12.
  2. 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 strings.Repeat(string(r), indent) + input         }         // Indent indenting the input by given indent         func Indent(input string, indent int) string {           padding := indent + len(input) return fmt.Sprintf("% "+strconv.Itoa(padding)+"s", ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required