golang 获取文件的编码
Go语言中可以使用第三方库github.com/saintfish/chardet来获取文件的编码。以下是一个示例代码:
package main
import (
"fmt"
"github.com/saintfish/chardet"
"io/ioutil"
)
func main() {
data, err := ioutil.ReadFile("test.txt")
if err != nil {
panic(err)
}
detector := chardet.NewTextDetector()
result, err := detector.DetectBest(data)
if err != nil {
panic(err)
}
fmt.Println(result.Charset)
}
以上代码使用ioutil.ReadFile读取文件内容,然后使用chardet.NewTextDetector()创建一个编码检测器,最后使用detector.DetectBest(data)获取文件的编码信息。result.Charset即为文件的编码
原文地址: https://www.cveoy.top/t/topic/hdKh 著作权归作者所有。请勿转载和采集!