-
Notifications
You must be signed in to change notification settings - Fork 7
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
Does len(string) might be a candidate? #4
Comments
Hi, sorry for the late response. I think that Although it might be confusing for people coming from other programming languages like Python (where unicode runes are counted by default), it is consistent in Golang, and I think that this paragraphs from the official docs let it clear:
So strings are just slices of bytes, and they know nothing about unicode, thus their behaviour is consistent with bytes slices. Also note, that |
Sure, fair enough! Thanks for your time 🙏 Happy to keep learn everyday 😇 |
I'm reopening this as I feel we can have a page for strings behaviour. While |
Additional context here. |
Just as a heads-up, I just hit a bug because of this. |
Even though it's properly documented (see here), IMHO the behaviour of the
len
built-in method for the case when the parameter is a string is a candidate for this repository.I assume there is a fair enough amount of reasons behind that could explain us why this behaviour was chosen, but I'd say that using the amount of bytes for strings is quite confusing.
Example:
So, as discussed here, the proper way to get the amount of characters within a given string is by using
len([]rune(string))
:Additionally, I'd say it could be interesting to open a new Go's proposal to include a wrapper function on the
strings
package. If the Go's spirit is keeping it simple & keeping backwards compatibility, I'd keep thelen
behaviour but I'd add that method, as therune
hack is not simple at all.PS: I'm not really really sure if the proposed method already exists, I only did a quick look up 😇
The text was updated successfully, but these errors were encountered: