Skip to content

strings: TrimLeft() returns unexpected value #67149

Closed as not planned
Closed as not planned

Description

@keisku

What is the URL of the page with the issue?

https://go.dev/play/p/5iv2wx09xc9

What is your user agent?

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36

Screenshot

2024-05-03_14-51-31

What did you do?

During debugging my program, I've noticed this unexpected behavior.

What did you see happen?

func main() {
	fmt.Println(strings.TrimLeft(
		"/kubernetes/kubernetes/release-1.30/api/openapi-spec/v3/apis__authentication.k8s.io__v1_openapi.json",
		"/kubernetes/kubernetes/release-1.30/api/openapi-spec/v3/",
	))
	// Expected: "apis__authentication.k8s.io__v1_openapi.json"
	// Actual:   "__authentication.k8s.io__v1_openapi.json"
}

What did you expect to see?

Wrote in the "What did you see happen?" section.

Now I am using filepath.Base() instead.
https://go.dev/play/p/sIBOz9OmLuV

Activity

  1. Jorropo commented on May 3, 2024

    @Jorropo
    Member

    TrimLeft takes a list of unicode codepoints to remove, this code is identical to:

    func main() {
    	fmt.Println(strings.TrimLeft(
    		"/kubernetes/kubernetes/release-1.30/api/openapi-spec/v3/apis__authentication.k8s.io__v1_openapi.json",
    		"/kuberntsla-1.30piocv",
    	))
    }

    You want:

    To remove a prefix, use TrimPrefix instead.

  2. locked and limited conversation to collaborators on May 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions