[Golang] Check If The Rune is Chinese Character


Given a rune value, check if the rune is a Chinese character.

Run Code on Go Playground

package main

import (
      "fmt"
      "unicode"
)

func main() {
      str := "hello, 世界"
      for _, runeValue := range str {
              if unicode.Is(unicode.Han, runeValue) {
                      fmt.Println(string(runeValue), "is a Chinese character")
              } else {
                      fmt.Println(string(runeValue), "is not a Chinese character")
              }
      }
}

Output

h is not a Chinese character
e is not a Chinese character
l is not a Chinese character
l is not a Chinese character
o is not a Chinese character
, is not a Chinese character
  is not a Chinese character
世 is a Chinese character
界 is a Chinese character

Tested on: Go Playground


References:

[1][Golang] Iterate Over UTF-8 Strings (non-ASCII strings)
[2]unicode - The Go Programming Language
[3][Golang] First Letter of Chinese Character Pinyin
[4][Golang] Calculate String Length
[5][Golang] Get UTF-8 String Width
[6][Golang] Sort String by Character
[7]I need help understanding runes : golang