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

40+ practical string tips [cheat sheet]

Share this page: