开源软件github.com/saintfish/chardet提供了检测文件编码的功能。git
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
检测输入的字节流中是否出现了某个特定编码才有的字符。github