Go strings.TrimRight
Remove whitespace at the end of a record while preserving its leading margin.
A simple example
strings.TrimRight
- Start with " A "
- Do this Remove trailing spaces
- Result Two spaces then A
package main
import (
"fmt"
"strings"
)
func main() {
value := " A "
fmt.Println("[" + strings.TrimRight(value, " \t") + "]")
}
[ A]
Syntax
"[" + strings.TrimRight(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.goKeep the indentation of a timetable
A copied checklist or timetable contains meaningful indentation. Adjust the requested leading margin and preserve the subordinate levels so the printed layout still communicates its structure.
package main
import (
"fmt"
"strings"
)
func main() {
value := " Math "
fmt.Println("[" + strings.TrimRight(value, " \t") + "]")
}
[ Math]
The two spaces before Math survive.
Clean the last field on a receipt
A terminal appends padding or a line ending to the final field it exports. Remove the trailing characters before comparing or displaying the value, while retaining the beginning of the field.
package main
import (
"fmt"
"strings"
)
func main() {
value := "paid\t"
fmt.Println("[" + strings.TrimRight(value, " \t") + "]")
}
[paid]
The trailing tab is removed.
Clean a course import
A course name arrives padded on both sides. Remove only the trailing spaces. Brackets show any indentation left at the front.
What to watch for
The examples use ASCII padding. Check the exact whitespace definition for imported Unicode text.