Go语言检测文件编码的方法
开源软件github.com/saintfish/chardet提供了检测文件编码的功能。
安装
go get github.com/saintfish/chardet
检测编码
rawBytes := []byte("some text") detector := chardet.NewTextDetector() charset, err := detector.DetectBest(rawBytes) if err != nil { panic(err) } println(charset.Charset) println(charset.Language)
支持的编码
UTF-8 GB-18030 ISO-8859-1 UTF-16BE UTF-16LE UTF-32BE UTF-32LE Big5
编码检测方法
检测输入的字节流中是否出现了某个特定编码才有的字符。
修改记录
- 2018年11月16日 建立文档。