-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
path/filepath: Split should trim trailing path separator else a Split stalemate occurs. #9928
Comments
If you want to walk a path, use filepath.Dir. Note that the document of filepath.Split makes the behavior very clear: Note the last sentence. |
So what happens when I want to extract a dir as well as file from a path that may contain a trailing '/'? Would I have to first TrimRight for every string or instead perform two separate calls? dir, file := filepath.Dir(p), filepath.Base(p) I see your response directly helps with walking but the last sentence doesn't fit my issue which is that the dir and file should then be extracted. In the original examples, if I wanted dir, file, I would still be stuck in that infinite loop, something a user that hasn't investigated this issue would not be aware of unless they performed a trim or two separate function calls. |
We cannot change the semantics of |
Call |
@adg alright, but if there was a way of letting some folks know about it. I realize that it isn't Go's behavior to make the transition from other languages to using Go intuitive, and probably change semantics just for that case since Go has its own docs and specs. However, it took me a lot of hours and a couple of bugs reported in a program that I am using this for, for me to investigate it. I was expecting filepath.Split to be deterministic the way other languages have it, regardless of whether there was a trailing '/' or not. Correction: * Go's responsibility instead of Go's behavior. |
I sent a change to add an example for the Spit function to make its behaviour more obvious: |
Thank you @adg. At least this example will leave a hint. |
Fixes #9928 Change-Id: Iab37051078755a132f211ad48e756422f7c55a39 Reviewed-on: https://go-review.googlesource.com/5416 Reviewed-by: Minux Ma <minux@golang.org>
filepath.Split returns a dir and base when given a path 'p'. If the path has no trailing separator, it behaves as expected producing dir and base. If p has a trailing path separator e.g 'p/', filepath.Split should return 'p' as the dir, and '' as the base. However, filepath.Split always returns the original value as the dir and '' as the base value, instead of 'p', '' e.g in http://play.golang.org/p/fswqGij8Hu
This behaviour causes an infinite loop when walking up a path. My use case is trying to find the least non existant path starting from the end of a path. In the code excerpt below:
Please contrast this with the behaviour of Python
The text was updated successfully, but these errors were encountered: