How to Convert String to Slice of Characters in Go
In this article, I’ll share with you a few ways to break a string into characters in Go:
- Using strings.Split()
- Using rune() – this is my preferred approach
- Using byte()
1. Break String into Character Substrings using strings.Split()
Given a string and a delimiter, strings.Split() slices the string into a slice of substrings.
When the delimiter is empty, the function returns a slice of the individual Unicode characters that make up the string instead.
[Example]
Note: Package strings need to be imported.
[Output]
This approach is helpful when you want the result to be a slice of strings.