-2

I try to write a program that takes as input a phrase and makes up an abbreviation using the first letters of the words. So, "Today I learned" → "TIL" or "Высшее учебное заведение" → "ВУЗ". I dont know how to deal with cyrillic alphabet, so my logic now is to get fields of string and convert it to runes, but the question is how to convert

    s := "Высшее учебное заведение"
    arr := strings.Fields(s)
    var runes []rune
    for i := range arr {
        runes = append(runes, rune(arr[i][0]))
    }
    fmt.Println(runes) // here I get bytes [208 209 208] but expect to get runes 
Victor
  • 33
  • 7
  • Here is the answer to your question https://stackoverflow.com/a/30263910/3201891 – Alex Aug 06 '22 at 15:38
  • 1
    Element by element. – Volker Aug 06 '22 at 15:50
  • 1
    Note that `[]string` is multiple strings, and each string is itself an immutable `[]byte`, more or less. So `[]string` is a whole lot like `[][]byte`. But `[]rune` is not: it's a single slice (of runes), not a slice-of-slices (of whatevers). Fortunately you're not interested in doing the impossible: you're converting from `string` (one slice, not slice-of-string = slice-of-slices) to `[]rune` (another single slice). You're just doing it for particular selected utf-8 sequences sub-setted out of the original string. – torek Aug 06 '22 at 17:34

1 Answers1

1

You can convert a string to []rune easily:

r := []rune("ABc€")

If you want to get runes, you should use %U format in print:

fmt.Printf("%U\n", r)