How to convert a piece of string to a rune slice

how to convert type []string

to []rune

?

I know you can do it like this:
[]rune(strings.Join(array,""))


 but is there a better way?

+3


source to share


1 answer


I would rather not use it strings.Join(array,"")

for this purpose because it creates one big newline which I don't need. Making a large string that I don't need is not cost effective, and depending on input and hardware, it can be time inefficient.

So, instead, I'll loop through the array of string values ​​and convert each string to a rune slice and use the built-in variational addition function to grow my slice of all rune values:



var allRunes []rune
for _, str := range array {
    allRunes = append(allRunes, []rune(str)...)
}

      

+4


source







All Articles