I'm teaching myself Go from a C background.
The code below works as I expect (the first two Printf() will access bytes, the last two Printf() will access codepoints).
What I am not clear is if this involves any copying of data.
package main
import "fmt"
var a string
func main() {
a = "èe"
fmt.Printf("%d\n", a[0])
fmt.Printf("%d\n", a[1])
fmt.Println("")
fmt.Printf("%d\n", []rune(a)[0])
fmt.Printf("%d\n", []rune(a)[1])
}
In other words:
does
[]rune("string")create an array of runes and fill it with the runes corresponding to"string", or it's just the compiler that figures out how to get runes from the string bytes?
It involves a copy because:
[]rune(s)didn't make a copy, you would be able to index the rune slice and change the string contentsstringvalue is a "(possibly empty) sequence of bytes", wherebyteis an alias ofuint8, whereas aruneis a "an integer value identifying a Unicode code point" and an alias ofint32. The types are not identical and even the lengths may not be the same: