Home > Backend Development > Golang > What\'s the difference between ranging over strings and rune slices in Go?

What\'s the difference between ranging over strings and rune slices in Go?

Susan Sarandon
Release: 2024-10-29 22:12:02
Original
831 people have browsed it

What's the difference between ranging over strings and rune slices in Go?

The Distinction Between Ranging Over Strings and Rune Slices

In Go, iterating using range over strings and rune slices might seem identical at first glance, as both yield the Unicode code points of the respective data structure. However, there's a crucial difference that becomes apparent when dealing with multibyte characters.

Ranging Over Strings

When you range over a string directly, like in the following code:

<code class="go">for _, s := range str {
    fmt.Printf("type of v: %s, value: %v, string v: %s \n", reflect.TypeOf(s), s, string(s))
}</code>
Copy after login

You're actually iterating over a sequence of bytes. As Go strings are essentially byte arrays, each iteration yields a byte from the string. This granularity may not pose an issue for strings primarily containing ASCII characters. However, for Unicode strings containing multibyte characters, byte-wise iteration could lead to unexpected results.

Ranging Over Rune Slices

In contrast, ranging over a rune slice, created by explicitly converting a string to a slice of runes like:

<code class="go">for _, s := range []rune(str) {
    fmt.Printf("type : %s, value: %v ,string : %s\n", reflect.TypeOf(s), s, string(s))
}</code>
Copy after login

Provides you with an iteration over code points. Unlike strings, rune slices are sequences of Unicode characters, making them more suitable for operating on textual data at the character level.

Implications for Indexing

The choice between ranging over strings and rune slices becomes even more critical when using indexing. Indexing a string will give you the byte position of a character, while indexing a rune slice will provide the index of the character within the sequence of code points.

For example, if you have a string with a multibyte character at index 1, indexing it as a rune slice would provide the index of that character, which may be different from the byte index.

Conclusion

In Go, ranging over strings and rune slices serves different purposes. Ranging over strings gives you bytes, while ranging over rune slices provides character-level iteration. The decision between the two depends on whether you need to work with bytes or characters, and whether indexing is a factor. For general-purpose text manipulation, rune slices are the preferred choice, ensuring consistent character-based operations regardless of character encoding.

The above is the detailed content of What\'s the difference between ranging over strings and rune slices in Go?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template