3

[Golang] First Letter of Chinese Character Pinyin

 2 years ago
source link: https://siongui.github.io/2017/05/05/go-chinese-character-pinyin-first-letter/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

[Golang] First Letter of Chinese Character Pinyin

May 05, 2017

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:

[2]pinyin - Go libraries and apps[3]GitHub - mozillazg/go-pinyin: 汉字拼音转换工具 Go 版[5][Golang] Iterate Over UTF-8 Strings (non-ASCII strings)[6]gosimple/slug: URL-friendly slugify with multiple languages support. : golang


Author: Siong-Ui Te Category: Go

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK