#development #golang

As a follow-up to my previous post on string length and byte size in PHP, I wanted to share how to do the same in Go.

To count the number of bytes in a string using Go, you can use the len function:

 1package main
 2
 3import (
 4    "fmt"
 5)
 6
 7func main() {
 8    data := "السلام علیکم ورحمة الله وبرکاته!"
 9    fmt.Println("bytes:", len(data))
10}
11
12// output:
13// bytes: 59

To count the number of characters in the string, assuming they are encoded in UTF-8, you can use the utf8.RuneCountInString function.

 1package main
 2
 3import (
 4    "fmt"
 5    "unicode/utf8"
 6)
 7
 8func main() {
 9    data := "السلام علیکم ورحمة الله وبرکاته!"
10      fmt.Println("runes =", utf8.RuneCountInString(data))
11}
12
13// Output:
14// runes = 32

I originally found this while reading Interview Questions for a Go Developer. Part 1: Fundamentals. It's nicely explained as:

In Go, you can obtain the length of a string in characters (runes) using the utf8.RuneCountInString function from the utf8 package. This function allows you to count the number of Unicode characters (runes) in a string, including multi-byte characters such as those from different languages and emojis.

It's important to remember that in Go, strings are sequences of bytes, and the length of a string in bytes might differ from its length in characters, especially when dealing with multi-byte characters. By using utf8.RuneCountInString, you can accurately determine the number of characters (runes) in a string regardless of their byte size.