Sling Academy
Home/Golang/Converting Strings to Runes and Vice Versa in Go

Converting Strings to Runes and Vice Versa in Go

Last updated: November 24, 2024

In the Go programming language, handling text efficiently and accurately is crucial for building reliable applications. A key concept in text processing with Go is the conversion between strings and runes. This article will explore how to perform these conversions effectively, starting from the basics to more advanced use cases.

Understanding Runes in Go

Before diving into conversions, it's important to understand what a rune represents. In Go, a rune is an alias for int32 and is used to represent a Unicode code point. Unlike ASCII, which uses one byte per character, Unicode can handle characters from various languages using code points that may require more bytes depending on the encoding.

Basic Conversion: String to Runes

The simplest way to convert a string to a slice of runes is to use a type conversion:

package main

import (
    "fmt"
)

func main() {
    str := "Hello, 世界"
    runes := []rune(str)
    fmt.Println(runes)
}

In this example, the string str is converted into a slice of runes, where each rune represents a Unicode code point of the original string characters.

Basic Conversion: Runes to String

Conversely, converting a slice of runes back into a string is equally straightforward:

package main

import (
    "fmt"
)

func main() {
    runes := []rune{72, 101, 108, 108, 111, 44, 32, 19990, 30028}
    str := string(runes)
    fmt.Println(str)
}

This code will output the original string by converting the slice of runes back to a string.

Intermediate Example: Iterating Over a String

When you need to iterate over each character of a string, it's recommended to use a range-based for loop that deals with runes:

package main

import (
    "fmt"
)

func main() {
    str := "Go is 猫"

    for index, runeValue := range str {
        fmt.Printf("Index %d: Rune %#U
", index, runeValue)
    }
}

Here, each character, represented by a rune, is accessed without worry about splitting multi-byte characters or invalid sequences.

Advanced Use Case: Handling Surrogate Pairs

Even though Go natively handles Unicode efficiently, knowing how to deal with surrogate pairs (in UTF-16 use cases) can sometimes be necessary when interacting with external systems.

package main

import (
    "fmt"
    "unicode/utf16"
)

func main() {
    runes := []rune{0xD835, 0xDC1D} // U+1D11D MUSICAL SYMBOL G CLEF
    utf16Encoding := utf16.Encode(runes)

    // To decode back
    decodedRunes := utf16.Decode(utf16Encoding)
    for _, runeValue := range decodedRunes {
        fmt.Printf("Decoded Rune: %#U
", runeValue)
    }
}

Using the unicode/utf16 package, you can effectively handle transformations involving surrogate pairs, ensuring correctness.

Conclusion

Understanding how to convert between strings and runes in Go enables developers to handle textual data more effectively and supports writing internationalized applications. Whether dealing with single-byte, multi-byte characters, or surrogate pairs, having these skills is essential for Go developers.

Next Article: Using the `strings` Package for Common String Operations

Previous Article: String Length in Go: Counting Characters vs Bytes

Series: Working with Strings in Go

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant