//下載外掛程式工具
go get -v github.com/gpmgo/gopm
//自動匯入包外掛程式
gopm get -g -v golang.org/x/tools/cmd/goimports
//轉碼外掛程式
gopm get -g -v golang.org/x/text
//自動檢測網頁編碼
gopm get -g -v golang.org/x/net/html
package mainimport ( "net/http" "io/ioutil" "fmt" "golang.org/x/text/transform" "io" "golang.org/x/text/encoding" "golang.org/x/net/html/charset" "bufio" "regexp")func main() { resp, err := http.Get("http://www.zhenai.com/zhenghun") if err != nil { panic(err) } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { fmt.Println("Error:status code", resp.StatusCode) return } e := determinEncoding(resp.Body) utf8Reader := transform.NewReader(resp.Body, e.NewDecoder()) all, err := ioutil.ReadAll(utf8Reader) if err != nil { panic(err) } printCityList(all)}func printCityList(contents []byte) { re := regexp.MustCompile(`<a href="(http://www.zhenai.com/zhenghun/[0-0a-z]+)"[^>]*>([^<]+)</a>`) matches := re.FindAllSubmatch(contents, -1) for _, m := range matches { fmt.Printf("City: %s, URL: %s\n", m[2], m[1]) } fmt.Printf("Matches found:%d\n", len(matches))}func determinEncoding(r io.Reader) encoding.Encoding { bytes, err := bufio.NewReader(r).Peek(1024) if err != nil { panic(err) } e, _, _ := charset.DetermineEncoding(bytes, "") return e}