[Golang] Convert Chinese Characters in String to Pinyin

Given a string with Chinese characters, convert the Chinese characters in the string to Pinyin (romanization system for Chinese) in Go.

For example,

Hello 世界

will become the following text after conversion.

Hello shijie

To do this, we need go-pinyin package to convert Chinese characters to Pinyin. Install the package first:

$ go get -u github.com/mozillazg/go-pinyin

Now we can convert Chinese characters to Pinyin in the string as follows:

import (


var a = pinyin.NewArgs()

func zhCharToPinyin(p string) (s string) {
      for _, r := range p {
              if unicode.Is(unicode.Han, r) {
                      s += string(pinyin.Pinyin(string(r), a)[0][0])
              } else {
                      s += string(r)


import (

func ExampleZhCharToPinyin() {
      fmt.Println(zhCharToPinyin("Hello 世界"))
      // Output: Hello shijie

Tones can be included in Pinyin by configuring Args. See README of go-pinyin package for more details.

Tested on: Ubuntu 18.04, Go 1.10.1


[1][Golang] First Letter of Chinese Character Pinyin
[2][Golang] Check If The Rune is Chinese Character
[3]gosimple/slug: URL-friendly slugify with multiple languages support. : golang