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/recipe11.
  2. Navigate to the directory.
  3. Create a file named whitespace.go with the following content:
        package main        import (          "fmt"          "math"          "regexp"          "strconv"          "strings"        )        func main() {          stringToTrim := "\t\t\n Go \tis\t Awesome \t\t"          trimResult := strings.TrimSpace(stringToTrim)          fmt.Println(trimResult)          stringWithSpaces := "\t\t\n Go \tis\n Awesome \t\t"          r := regexp.MustCompile("\\s+")          replace := r.ReplaceAllString(stringWithSpaces, " ")          fmt.Println(replace)          needSpace := "need space"          fmt.Println(pad(needSpace, 14, "CENTER"))          fmt.Println(pad(needSpace, 14, "LEFT"))        }        func pad(input string, padLen int, align string) string {          inputLen := len(input)          if inputLen >= padLen { return input ...

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