Skip to content

Instantly share code, notes, and snippets.

@xigang
Created May 16, 2017 05:41
Show Gist options
  • Save xigang/189a9075ac1d1a79b5ca07cc12c101f3 to your computer and use it in GitHub Desktop.
Save xigang/189a9075ac1d1a79b5ca07cc12c101f3 to your computer and use it in GitHub Desktop.
这两个函数是针对单个rune和[]byte之间的转换。
utf8编码时,一个字符可能需要1、2、3或4个字节表示;在go中,一个utf8字符用rune类型表示;所以,这里的Encode和Decode是针对一个rune到[]byte的转换。
如下代码示例:
package main
import (
"fmt"
"unicode/utf8"
)
func main() {
r := '中'
p := make([]byte, 3)
n := utf8.EncodeRune(p, r)
fmt.Printf("bytes: %v, nums: %d\n", p, n)
newRune, n := utf8.DecodeRune(p)
fmt.Printf("rune: %c, nums: %d\n", newRune, n)
}
输出:
bytes: [228 184 173], nums: 3
rune: 中, nums: 3
至于说怎么实现你要求的功能,代码如下:
package main
import (
"fmt"
"io/ioutil"
)
func main() {
readbuf, _ := ioutil.ReadFile("test.txt")
str := string(readbuf)
for _, word := range str {
fmt.Printf("%c\t", word)
}
}
不过,如果文件不是utf8编码,这样读出来会乱码。这个时候需要进行编码转换,可以看看:Go中进行字符集转换。(http://studygolang.com/resources/42)
题外话,如果文件很大,最好别这么一次性读取内容。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment