
Golang's strings.TrimSpace function is a powerful tool for cleaning up whitespace from strings. It removes all leading and trailing whitespace from a string.
Removing whitespace from strings is a common task, especially when working with user input or data from external sources. In Golang, you can use strings.TrimSpace to achieve this.
The strings.TrimSpace function is case-sensitive and will not remove whitespace from the middle of a string. For example, " hello world " will be trimmed to "hello world" but "hello world" will remain unchanged.
Broaden your view: Golang Generic Function
Go Strings
', '\v', '\f', '\r', ' ' (the space character), 0x85, and 0xA0 as whitespace characters.
The strings.TrimSpace() method takes one parameter, which is the string with either leading or trailing white spaces. It returns a string without leading and trailing spaces. This method is useful for cleaning up strings that have extra whitespace.
The strings.TrimSpace() method can handle strings with leading and trailing unicode points, but it will leave them behind after removing whitespace. To remove these points, you'll need to use another method.
Recommended read: Golang Concat String
Duplicate Whitespace in Go
Trimming duplicate whitespace in Go can be a breeze. We'll use the strings package to help us achieve this.
The strings.Fields() function is perfect for this task. It splits a string into a slice of strings around consecutive whitespace characters.
This function uses the same whitespace characters as strings.TrimSpace() and our own left and right trim functions, which are defined by unicode.IsSpace().
Once we have a slice of strings with no whitespace characters, we can join them all into a single string with spaces in between using strings.Join().
Recommended read: Golang Replace Characters in String
Strings
Strings in Go are represented as a sequence of bytes, making them immutable and unchangeable once created.
You can create a new string using double quotes (“”) or backticks (`).
The strings package from the standard library has a function that removes leading and trailing whitespace from a string: strings.TrimSpace().
This function considers any of '\t', '
', '\v', '\f', '\r', ' ', 0x85, and 0xA0 as whitespace characters.
A different take: Golang Strings Package
You can use strings.TrimSpace() to remove new lines, tabs, and whitespace from the beginning and end of a string.
The strings.TrimSpace() function takes one parameter, the string with leading or trailing whitespace, and returns a string without leading and trailing spaces.
It preserves the space character between words, like the space between "Ice" and "Cream".
Trimming Whitespace
', '\v', '\f', '\r', ' ', 0x85, and 0xA0 characters.
The strings package doesn't have a TrimLeftSpace() function, so we have to write our own. Fortunately, the other trim functions make it easy. We can use strings.TrimFunc() to create a custom trim function.
To remove whitespace on the right side of a string, we can swap strings.TrimLeftFunc() for strings.TrimRightFunc(). This is as simple as swapping the two functions.
There are three main functions to help us write our own trim functions: strings.TrimFunc(), strings.TrimLeftFunc(), and strings.TrimRightFunc(). They all take a function as their second argument that determines whether to remove a character.
Expand your knowledge: Golang Strings Trim
Here are the main trim functions in the strings package:
- strings.TrimSpace() - removes leading and trailing whitespace
- strings.TrimFunc() - removes characters based on a function
- strings.TrimLeftFunc() - removes characters from the left side
- strings.TrimRightFunc() - removes characters from the right side
The strings.TrimLeft() function works almost exactly like strings.Trim(), but only removes characters from the left side. This can be helpful for removing leading whitespace from a string.
Using the Strings Function
Golang's strings package is a powerful tool for working with strings.
The strings.TrimSpace() function can be used to trim leading and trailing whitespace from a string.
You can use the strings.Trim() function to trim specific characters from the beginning and/or end of a string.
To remove spaces from the beginning and end of a string, you can pass " " as the second argument to the strings.Trim() function.
For more insights, see: Golang Reflect to Call Function in Package
Example and Output
The strings.TrimSpace() function is a game-changer for cleaning up strings in Go. It's simple to use and can be called with the input string to obtain a new string with the whitespace removed.
In the example provided, we see that this function is useful for strings that contain leading and trailing whitespace. The output of calling TrimSpace() on such a string results in a new string with the whitespace removed.
By using strings.TrimSpace(), you can easily remove unwanted whitespace from your strings and work with cleaner, more manageable data.
Example

Here's an example of using the strings.TrimSpace() function to trim a string. This function removes leading and trailing whitespace from a string, resulting in a cleaner and more efficient output.
The strings.TrimSpace() function is useful for removing extra spaces from user input, such as when a user enters their name with extra spaces around it.
Output
In the example, we have a string named str that contains leading and trailing whitespace. This is a common issue when working with strings in programming.
The strings.TrimSpace() function is used to remove the whitespace from the input string, resulting in a new string without the extra spaces. This function is a convenient solution for this problem.
The resulting string is free of leading and trailing whitespace, making it easier to work with in programming. This can be a big help when you're trying to get a string to behave in a specific way.
Consider reading: Golang Network Programming
Go Strings Functions
The Go strings package has a function called strings.TrimSpace() that removes leading and trailing whitespace from a string. This function is perfect for cleaning up strings that have been input by users or read from files.
You can use the strings.TrimSpace() function to remove whitespace characters like '\t', '
', '\v', '\f', '\r', ' ', 0x85, and 0xA0. The function is easy to use, and it's a great way to get rid of unwanted whitespace.
The strings.TrimSpace() function is available in the standard library, so you don't need to install any additional packages to use it. It's a convenient and efficient way to work with strings in Go.
Discover more: Go High Level Twilio Integration
String Conversion
String conversion in Go is a straightforward process, achieved through the use of the Sprintf function.
You can specify the format of the string by including placeholders for the values, such as %s for strings and %d for integers.
The Sprintf function returns a string, which can be assigned to a variable or printed directly to the console.
The format string can include multiple placeholders, allowing you to convert multiple values to a single string.
For example, the format string "%s, %d" can be used to convert a string and an integer to a string.
For another approach, see: Golang String Format
Go's Other Functions
Go's Other Functions are actually quite useful, and they're part of the Go standard library.
You can use these functions to remove any set of characters from the left or right side of a string, not just whitespace.
The three functions you should know are strings.Trim(), strings.TrimLeft(), and strings.TrimRight().
Frequently Asked Questions
How do you split a string by spaces in Go?
To split a string by spaces in Go, use the strings.Fields function, which divides the input string into substrings based on white space characters. This returns a slice of substrings that can be easily processed further.
Featured Images: pexels.com


