[Golang] First Letter of Chinese Character Pinyin


My answer to How can I get Chinese first letter(Pinyin) by using Go language? - Stack Overflow [4]

package first

import (
      "github.com/mozillazg/go-pinyin"
)

var a = pinyin.NewArgs()

func FirstLetterOfPinYin(r rune) string {
      result := pinyin.Pinyin(string(r), a)
      return string(result[0][0][0])
}

Testing:

package first

import (
      "testing"
)

func TestFirstLetterOfPinYin(t *testing.T) {
      if FirstLetterOfPinYin('世') != "s" {
              t.Error("世")
      }
      if FirstLetterOfPinYin('界') != "j" {
              t.Error("界")
      }
}

Tested on: Ubuntu Linux 17.04, Go 1.8.1.


References:

[1]
[2]pinyin - Go libraries and apps
[3]GitHub - mozillazg/go-pinyin: 汉字拼音转换工具 Go 版
[4]How can I get Chinese first letter(Pinyin) by using Go language? - Stack Overflow
[5][Golang] Iterate Over UTF-8 Strings (non-ASCII strings)