1. package main
    2. import "fmt"
    3. func testCalc(str string) (charCount, spaceCount, numberCount, otherCount int) {
    4. utfChars := []rune(str)
    5. for i := 0; i < len(utfChars); i++ {
    6. if (utfChars[i] >= 'a' && utfChars[i] <= 'z') || (utfChars[i] >= 'a' && utfChars[i] <= 'z') {
    7. charCount++
    8. continue
    9. }
    10. if utfChars[i] >= '0' && utfChars[i] <= '9' {
    11. numberCount++
    12. continue
    13. }
    14. if utfChars[i] == ' ' {
    15. spaceCount++
    16. continue
    17. }
    18. otherCount++
    19. }
    20. return
    21. }
    22. func testExample() {
    23. str := "dssd 世界 22"
    24. charCount,numCount,spCount,other := testCalc(str)
    25. fmt.Printf("charCount =%d,numCount = %d,spCount =%d,other = %d",charCount,numCount,spCount,other)
    26. }
    27. func main() {
    28. testExample()
    29. }

    输出结果:
    charCount =4,numCount = 2,spCount =2,other = 2