Automatically parse the encoding format of an HTML page:
Need to rely on the two external libraries of Golang.org/x/text and Golang.org/x/net
Package Mainimport ("net/http" "Io/ioutil" "FMT" "Golang.org/x/text/transform" "io" "golang.org/x/text/encoding" "Bufio" "Golang.org/x/net/html/charset")//encoding determine for HTML page, EG:GBK gb2312 GB18030func determineencoding (r io. Reader) encoding. Encoding {bytes, err:= Bufio. Newreader (R). Peek (1024x768) ifErr! =Nil {panic (err)} E, _, _:= CharSet. Determineencoding (Bytes,"") returnE}func Main () {resp, err:= http. Get ("Http://www.zhenai.com/zhenghun") ifErr! =Nil {panic (err)} Defer resp. Body.close () E:=determineencoding (resp. Body) Reader:=transform. Newreader (resp. Body, E.newdecoder ())ifResp. StatusCode = =http. Statusok {bodybytes, err:=Ioutil. ReadAll (reader)ifErr! =Nil {panic (err)} FMT. Printf ("%s\n", Bodybytes)}}
Golang HTML Encoding parsing