Go strings.TrimLeft
Remove leading spaces while preserving spacing at the other edge.
A simple example
strings.TrimLeft
- Start with " A "
- Do this Remove leading spaces
- Result A then two spaces
package main
import (
"fmt"
"strings"
)
func main() {
value := " A "
fmt.Println("[" + strings.TrimLeft(value, " \t") + "]")
}
[A ]
Syntax
"[" + strings.TrimLeft(value, " \t") + "]"
Run these examples in Go
Save one example at a time as main.go. Use Go 1.21 or later.
go run main.goAlign a pasted seat label
A user pastes a multi-line block from a formatted document into a plain-text field. Remove the common outer margin while preserving meaningful differences between the lines inside it.
package main
import (
"fmt"
"strings"
)
func main() {
value := " A-17 "
fmt.Println("[" + strings.TrimLeft(value, " \t") + "]")
}
[A-17 ]
The leading spaces vanish and the reserved space after 17 remains.
Clean a copied delivery field
A user pastes a name or code from another application. Remove the unwanted outer padding while preserving the spaces and letters that belong to the actual value.
package main
import (
"fmt"
"strings"
)
func main() {
value := "\tNorth Road"
fmt.Println("[" + strings.TrimLeft(value, " \t") + "]")
}
[North Road]
The tab at the beginning is removed.
Clean a course import
A course name arrives padded on both sides. Remove only the leading spaces. Brackets make any spaces left at the end visible.
What to watch for
The brackets in the output make significant trailing spaces visible.