開源軟件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