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
编码检测方法
检测输入的字节流中是否出现了某个特定编码才有的字符。
参考资料
- https://github.com/saintfish/chardet
修改记录
- 2018年11月16日 建立文档。
本文由 创作,采用 知识共享署名4.0 国际许可协议进行许可。本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为: 2020/08/09 04:09