Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strings.TrimRight unexpected output #41557

lafh opened this issue Sep 22, 2020 · 2 comments

strings.TrimRight unexpected output #41557

lafh opened this issue Sep 22, 2020 · 2 comments


Copy link

@lafh lafh commented Sep 22, 2020

What version of Go are you using (go version)?

$ go version
go version go1.15 linux/amd64

Does this issue reproduce with the latest release?


What operating system and processor architecture are you using (go env)?

go env Output
$ go env

What did you do?

package main

import (

func main() {
	fmt.Println(strings.TrimRight("", ""))


What did you expect to see?


What did you see instead?


Copy link

@ALTree ALTree commented Sep 22, 2020

In TrimRight, the second parameter is a cutset: TrimRight(s string, cutset string), i.e. a set of individual characters to be trimmed.

Passing "" doesn't mean "trim right at", it means "trim all @ x, c, o, and m characters from the right". That's why your c gets trimmed, too.

Working as intended, closing this.

@ALTree ALTree closed this Sep 22, 2020
Copy link

@ianlancetaylor ianlancetaylor commented Sep 22, 2020

By the way, you might be looking for strings.TrimSuffix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.