Strings and Text Processing
String Runes
len counts bytes, while converting to []rune counts Unicode code points.
String Runes
string_runes.go
package main
import "fmt"
func main() {
var text =
bytes := len(text)
runes := len([]rune(text))
fmt.Println("text=", text)
fmt.Println("bytes=", bytes)
fmt.Println("runes=", runes)
fmt.Println("sameCount=", bytes == runes)
}
package main
import "fmt"
func main() {
var text =
bytes := len(text)
runes := len([]rune(text))
fmt.Println("text=", text)
fmt.Println("bytes=", bytes)
fmt.Println("runes=", runes)
fmt.Println("sameCount=", bytes == runes)
}
package main
import "fmt"
func main() {
var text =
bytes := len(text)
runes := len([]rune(text))
fmt.Println("text=", text)
fmt.Println("bytes=", bytes)
fmt.Println("runes=", runes)
fmt.Println("sameCount=", bytes == runes)
}
rune count
Go strings store bytes, and runes let code count Unicode characters more directly.