I recently asked this question and the answers increased my understanding, but they didn't solve the actual problem I had. So, I will try to ask a similar but different question as follows.
Suppose that I want to access random rune
element of a string
. One way is:
func RuneElement(str string, idx int) rune {
var ret rune
for i, c := range str {
if i == idx {
return c
}
}
return ret // out of range -> proper handling is needed
}
What if I want to call such a function a lot of times? I guess what I am looking for is like an operator/function like str[i]
(which returns a byte
) that return the rune
element at i
-th position. Why this element can be accessed using for ... range
but not through a funtcion like str.At(i)
for example?