在Go中遍历unicode字符串时跳过n个代码点

In Go, iterating over a string using

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

only accesses individual bytes in the string, whereas iterating over a string via

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

iterates over individual Unicode codepoints (calledrunes in Go), which may span multiple bytes.

My question is: how does one go about jumping ahead while iterating over a string with range Mystring? continue can jump ahead by one unicode codepoint, but it's not possible to just do i += 3 for instance if you want to jump ahead three codepoints. So what would be the most idiomatic way to advance forward by n codepoints?

I asked this question on the golang nuts mailing list, and it was answered, courtesy of some of the helpful folks on the list. Someone messaged me however suggesting I create a self-answered question on Stack Overflow for this, to save the next person with the same issue some trouble. That's what this is.

I'd consider avoiding the conversion to []rune, and code this directly.

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

It looks inefficient to skip runes one by one like this, but it's the same amount of work as the conversion to []rune would be. The advantage of this code is that it avoids allocating the rune slice, which will be approximately 4 times larger than the original string (depending on the number of larger code points you have). Of course converting to []rune is a bit simpler so you may prefer that.

It turns out this can be done quite easily simply by casting the string into a slice of runes.

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