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:

package main

import (
    "fmt"
)

func main() {
    data := "السلام علیکم ورحمة الله وبرکاته!"
    fmt.Println("bytes:", len(data))
}

// output:
// 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.

package main

import (
  "fmt"
  "unicode/utf8"
)

func main() {
    data := "السلام علیکم ورحمة الله وبرکاته!"
    fmt.Println("runes =", utf8.RuneCountInString(data))
}

// Output:
// 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.