package charset

import (
	"fmt"
	"github.com/saintfish/chardet"
)

// DetectCharset 检测字符编码
func DetectCharset(data []byte) string {
	detector := chardet.NewTextDetector()
	result, err := detector.DetectBest(data)
	if err == nil {
		fmt.Printf(
			"Detected charset is %s, language is %s",
			result.Charset,
			result.Language)
		return result.Charset
	}

	return "unknown"
}
