How to Convert String to Rune in Go

To convert a string to a rune in Golang, you can use the “[]rune()”. A rune is a single Unicode code point. A string is a sequence of runes.




package main

import (

func main() {
  // Define a string
  s := "Hi, Golang!"

  // Convert the string to a slice of runes
  runeSlice := []rune(s)

  // Iterate through the slice of runes and print each rune
  // And its Unicode code point
  for i, r := range runeSlice {
    fmt.Printf("runeSlice[%d] = %c (U+%04X)\n", i, r, r)


runeSlice[0] = H (U+0048)
runeSlice[1] = i (U+0069)
runeSlice[2] = , (U+002C)
runeSlice[3] = (U+0020)
runeSlice[4] = G (U+0047)
runeSlice[5] = o (U+006F)
runeSlice[6] = l (U+006C)
runeSlice[7] = a (U+0061)
runeSlice[8] = n (U+006E)
runeSlice[9] = g (U+0067)
runeSlice[10] = ! (U+0021)

In this example, we defined a string s, then converted it to a slice of runes using a type conversion []rune(s).

The result is stored in the runeSlice variable. We then iterate through the runeSlice and print each rune and its corresponding Unicode code point.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.