3 ways to trim whitespace (or other characters) from a string

yourbasic.org/golang

Use the strings.TrimSpace function to remove leading and trailing whitespace as defined by Unicode.

s := strings.TrimSpace("\t Goodbye hair!\n ")
fmt.Printf("%q", s) // "Goodbye hair!"

Further reading

The String functions cheat sheet presents the top Go string functions.

Share this page: