Home > Backend Development > Golang > Go Strings: Rune vs. Byte Iteration: When Should I Use Which?

Go Strings: Rune vs. Byte Iteration: When Should I Use Which?

DDD
Release: 2024-12-16 13:39:11
Original
437 people have browsed it

Go Strings: Rune vs. Byte Iteration: When Should I Use Which?

Understanding the Difference Between Rune and Byte Ranging in Strings in Go

While iterating over a string in Go using the range clause, you may encounter two data types: rune and byte. This can be confusing, so let's delve into the details of why this occurs.

According to the Go language definition, strings are sequences of bytes, and their individual elements can be accessed by their index using str[index]. However, when you loop over a string using for range, the retrieved characters are of type rune.

This distinction arises from the definition of the range clause, which allows iteration over code points (Unicode characters) in the string. Each code point consists of one or more bytes, depending on the encoding of the character. The range clause iterates over the start bytes of these code points, providing the corresponding rune value as the second element in the tuple.

If you encounter an invalid UTF-8 sequence during iteration, the second value will be the Unicode replacement character (0xFFFD), and the iterator will advance by one byte.

To iterate over the individual bytes of a string, you can use the following alternatives:

  1. Manual Byte Looping:

    for i := 0; i < len(s); i++ {
        // Access the byte at position i in "s"
    }
    Copy after login
  2. Converting to a Byte Slice:

    for i, b := range []byte(s) {
        // Access both the index (i) and the byte (b)
    }
    Copy after login

While ranging over the bytes of a string is less concise, it provides access to the raw byte values, which may be necessary in certain scenarios. By understanding the distinction between rune and byte ranging, you can effectively manipulate strings in Go to meet your desired requirements.

The above is the detailed content of Go Strings: Rune vs. Byte Iteration: When Should I Use Which?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template