go統計字符串中漢字個數

package main

import (    
    "fmt"
    "unicode")
    
// HanCounter to count the number of chinese character.
func HanCounter(s string) int {
    var count int // 0 
    for _, c := range s {        
        if unicode.Is(unicode.Han, c) {     
            count++        
        }    
    }    
    return count
}

func main() {    
    s := "hello庫珀"    
    count := HanCounter(s)    
    fmt.Println(count)
}

一開始出現標黃exported function HanCounter should have comment or be unexported,需要在給函數添加註釋,且以函數名開頭,以句號結尾。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章