Golang下进行字符集转换
演示一下gb2312转UTF8
源网页是国家地震局的一周内地震统计,编码是gb2312
package main
import (
iconv "github.com/djimenez/iconv-go"
"io/ioutil"
"log"
"net/http"
)
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
resp, err := http.Get("http://data.earthquake.cn/datashare/globeEarthquake_csn.html")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
input, err := ioutil.ReadAll(resp.Body)
out := make([]byte, len(input))
out = out[:]
iconv.Convert(input, out, "gb2312", "utf-8")
ioutil.WriteFile("out.html", out, 0644)
}
blog comments powered by Disqus