Golang中的编码检测mahonia
原文链接: Golang中的编码检测mahonia
编码转换
[编码检测chardet/detector_test] github.com/saintfish/chardet
package gogo
import (
"fmt"
"strings"
"github.com/axgle/mahonia"
"github.com/saintfish/chardet"
)
// 自动检测编码,并转为utf8
func ToUTF8(buf string) string {
dt := chardet.NewTextDetector()
ret, err := dt.DetectBest([]byte(buf))
if err != nil {
return buf
}
fmt.Println(ret.Charset)
charset := strings.ToLower(ret.Charset)
if strings.HasPrefix(charset, "gb") {
enc := mahonia.NewEncoder("gbk")
return enc.ConvertString(buf)
}
return buf
}