Home > Backend Development > Golang > How Do I Iterate Over Runes in a Go String?

How Do I Iterate Over Runes in a Go String?

Barbara Streisand
Release: 2024-11-13 03:10:02
Original
852 people have browsed it

How Do I Iterate Over Runes in a Go String?

Iterating Over Strings by Runes in Go

When iterating over strings in Go, developers often encounter the need to work with runes (Unicode code points) rather than bytes. This is because Go's string type stores bytes, not runes.

Consider the following code snippet:

for i := 0; i < len(str); i++ {
    dosomethingwithrune(str[i]) // takes a rune
}
Copy after login

This snippet is intended to iterate over the characters in the string str, performing some action with each rune. However, accessing str[i] returns a byte, not a rune.

To iterate over the runes in a string, Go provides a range expression specifically for this purpose:

for pos, char := range "日本語" {
    fmt.Printf("character %c starts at byte position %d\n", char, pos)
}
Copy after login

In the example above, the range expression iterates over the runes in the string, assigning the current rune to the variable char and the current byte position to the variable pos.

The advantage of the range expression is that it handles UTF-8 decoding automatically, breaking out individual Unicode code points. This simplifies the task of iterating over the runes in a string while avoiding the potential pitfalls of manual iteration.

The above is the detailed content of How Do I Iterate Over Runes in a Go String?. 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