Skip forward n code points when iterating through a Unicode string in Go

In Go, iterating over a string using

for i := 0; i < len(myString); i++{ 
    doSomething(myString[i])
}

only accesses individual bytes in the string, and iterating through the string through

for i, c := range myString{ 
    doSomething(c)
}

iterates over individual Unicode code points (called runeGo), which can span multiple bytes.

My question is: how can I go forward iterating through a string using range Mystring? continuecan go forward with one unicode code, but it’s not possible to just do i += 3it if you want to jump forward at three code points. So, what would be the most idiomatic way of moving forward n code points?

I golang nuts, , . - , , " " , . .

+4
2

[]rune .

skip := 0
for _, c := range myString {
    if skip > 0 {
        skip--
        continue
    }
    skip = doSomething(c)
}

, , []rune. , , 4 , ( , ). , []rune , .

+6

, , .

runes := []rune(myString)
for i := 0; i < len(runes); i++{
    jumpHowFarAhead := doSomething(runes[i])
    i += jumpHowFarAhead
}
+2

Source: https://habr.com/ru/post/1537261/


All Articles