Trimming Spaces in Go: How to Get Rid of Extra Characters in a String

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

A Problem Familiar to Every Developer

\n\n

Imagine: you're writing a log parser, processing user input from a form, or preparing data for an API. Strings come with extra spaces at the beginning and end. If you don't remove them — bugs, broken comparisons, malformed JSON, and hours of debugging lost. This is a classic problem that has gathered thousands of views on Stack Overflow. Today we'll solve it in Go — quickly, elegantly, and without surprises.

\n\n

In this guide, you will learn:

\n
    \n
  • How to use the standard strings library for trimming spaces.
  • \n
  • How TrimSpace differs from TrimLeft and TrimRight.
  • \n
  • How to write your own custom function for non-standard whitespace characters.
  • \n
  • How to avoid pitfalls with Unicode and tabulation.
  • \n
\n\n

Let's go!

\n\n

Quick Start: strings.TrimSpace

\n\n

The simplest and most popular way to remove spaces from both ends of a string is to use the strings.TrimSpace function from the standard strings package. It removes all characters that Go considers "whitespace": regular spaces, tabs, newlines, carriage returns, and even vertical tabs.

\n\n

Here's what it looks like in code:

\n\n
package main\n\nimport (\n    "fmt"\n    "strings"\n)\n\nfunc main() {\n    raw := "   Hello, world!   \\n\\t"\n    cleaned := strings.TrimSpace(raw)\n    \n    fmt.Printf("Original: %q\\n", raw)\n    fmt.Printf("Cleaned: %q\\n", cleaned)\n}\n
\n\n

Output:

\n
Original: "   Hello, world!   \\n\\t"\nCleaned: "Hello, world!"\n
\n\n

Note: %q in fmt.Printf displays the string with escape sequences — this is convenient for debugging to see hidden characters.

\n\n

When to use: in 90% of cases, this is enough. If you just need to "clean" a string of junk at the edges — use TrimSpace.

\n\n

Fine-Tuning: TrimLeft and TrimRight

\n\n

Sometimes you need to trim spaces only on the left or only on the right. For this, Go has strings.TrimLeft and strings.TrimRight. But there's a nuance: they don't work with "whitespace characters" by default, but with a given set of characters.

\n\n

Example:

\n\n
package main\n\nimport (\n    "fmt"\n    "strings"\n)\n\nfunc main() {\n    s := "   \\t\\n   Hello   "\n    \n    // Trim only from the left (spaces, tabs, newlines)\n    left := strings.TrimLeft(s, " \\t\\n")\n    fmt.Printf("Left: %q\\n", left)\n    \n    // Trim only from the right (only spaces)\n    right := strings.TrimRight(s, " ")\n    fmt.Printf("Right: %q\\n", right)\n}\n
\n\n

Output:

\n
Left: "Hello   "\nRight: "   \\t\\n   Hello"\n
\n\n

Important: TrimLeft and TrimRight remove all characters from the second argument until they encounter a character that is not in it. If you specify only a space " ", then a tab \\t will remain untouched.

\n\n

TrimSpace Equivalent for One Side

\n\n

If you need to trim all whitespace only from the left or only from the right, but don't want to list all the characters, you can write a small wrapper:

\n\n
func trimLeftSpace(s string) string {\n    return strings.TrimLeft(s, " \\t\\n\\r\\v\\f")\n}\n\nfunc trimRightSpace(s string) string {\n    return strings.TrimRight(s, " \\t\\n\\r\\v\\f")\n}\n
\n\n

Or, for those who like brevity, use strings.TrimSpace with a trick:

\n\n
// Trim only from the left: first reverse, then TrimSpace, then reverse back\nfunc trimLeftSpace(s string) string {\n    runes := []rune(s)\n    for i, r := range runes {\n        if !unicode.IsSpace(r) {\n            return string(runes[i:])\n        }\n    }\n                

Blogs

Book Recommendations