-
Notifications
You must be signed in to change notification settings - Fork 18k
strings: FieldFunc behaves differently from Split #72841
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
Comments
If they were the same we wouldn't need 2 different functions. |
Well, no, the two functions would still be different. They would definitely not collapse into one. The point is that
|
I agree with @seankhliao that the documentation of Fields and FieldsFunc could be more explicit about leading and trailing spaces, and that neither function can be changed at this point. It sounds like you wish there was a third function in the library. Feel free to propose one. |
I agree with both of you that the best solution is to add a few words about this unexpected behaviour (i.e. bug) to the documentation. As for the missing functions: I already wrote two functions |
Change https://go.dev/cl/658218 mentions this issue: |
Go version
go version go1.24.1 windows/amd64
Output of
go env
in your module/workspace:What did you do?
I wanted to split a string on several different characters, not only one, so I replaced a call of
strings.Split(encodings, ":")
bystrings.FieldFunc(encodings, separatorFunc)
whereseparatorFunc
was this:What did you see happen?
With
encodings := ":utf16bom"
the calls had two different results:Split
: Slice with two elements[ "" "utf16bom" ]
FieldFunc
: Slice with one element[ "utf16bom" ]
What did you expect to see?
With
encodings := ":utf16bom"
the calls should have delivered identical results:Split
: Slice with two elements[ "" "utf16bom" ]
FieldFunc
: Slice with two elements[ "" "utf16bom" ]
The text was updated successfully, but these errors were encountered: