Convert between rune array/slice and string

yourbasic.org/golang
Golang gopher

Convert string to runes

r := []rune("ABC€")
fmt.Println(r)        // [65 66 67 8364]
fmt.Printf("%U\n", r) // [U+0041 U+0042 U+0043 U+20AC]

You can also use a range loop to access the code points of a string.

Convert runes to string

s := string([]rune{'\u0041', '\u0042', '\u0043', '\u20AC', -1})
fmt.Println(s) // ABC€�

Performance

These conversions create a new slice or string, and therefore have time complexity proportional to the number of bytes that are processed.

More efficient alternative

In some cases, you might be able to use a string builder, which can concatenate strings without redundant copying:

Efficient string concatenation [full guide]

Further reading

40+ practical string tips [cheat sheet]

Share this page: