package main
import (
"bufio"
"fmt"
"golang.org/x/net/html/charset"
"golang.org/x/text/transform"
"io"
"io/ioutil"
"net/http"
)
func DetermineEncoding(r io.Reader) []byte {
OldReader := bufio.NewReader(r)
bytes, err := OldReader.Peek(1024)
if err != nil {
panic(err)
}
e, _, _ := charset.DetermineEncoding(bytes, "")
reader := transform.NewReader(OldReader, e.NewDecoder())
all, err := ioutil.ReadAll(reader)
if err != nil {
panic(err)
}
return all
}
func main() {
resp, err := http.Get(`http://www.chinanews.com`)
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Printf(`%s`, DetermineEncoding(resp.Body))
}
有疑问加站长微信联系(非本文作者)