常用函数

  • RuneCountlnString: 获取字符串码点的数量

    操作

    learnUtf8.go

    ```go package learnUtf8

import ( “fmt” “unicode/utf8” )

func LearnUtf8() { fmt.Println(utf8.RuneCountInString(“abcdef我”)) fmt.Println(utf8.RuneCountInString(“abcdef”)) fmt.Println(len(“abcdef我”)) fmt.Println(len(“abcdef”)) }

  1. <a name="t10S0"></a>
  2. ## learnUtf8_test.go
  3. ```go
  4. package learnUtf8
  5. import "testing"
  6. func TestLearnUtf8(t *testing.T) {
  7. LearnUtf8()
  8. }

执行结果

  1. GOROOT=C:\Program Files\Go #gosetup
  2. GOPATH=C:\Users\ligz\go #gosetup
  3. "C:\Program Files\Go\bin\go.exe" test -c -o C:\Users\ligz\AppData\Local\Temp\GoLand\___basicproject_learnUtf8__TestLearnUtf8.test.exe basicproject/learnUtf8 #gosetup
  4. "C:\Program Files\Go\bin\go.exe" tool test2json -t C:\Users\ligz\AppData\Local\Temp\GoLand\___basicproject_learnUtf8__TestLearnUtf8.test.exe -test.v -test.paniconexit0 -test.run ^\QTestLearnUtf8\E$ #gosetup
  5. === RUN TestLearnUtf8
  6. 7
  7. 6
  8. 9
  9. 6
  10. --- PASS: TestLearnUtf8 (0.00s)
  11. PASS
  12. 进程 已完成,退出代码为 0