废话不多说,直接上代码。
package main import ( "errors" "fmt" "io/ioutil" "net/http" "net/url" ) const ( Host = "http://ip.taobao.com" Addr = Host + "/service/getIpInfo.php?" ) func taobao(ip string) (string, error) { values := url.Values{} values.Add("ip", ip) // client url := Addr + values.Encode() resp, err := http.Get(url) if err != nil { return "", err } if resp != nil && resp.Body != nil { defer resp.Body.Close() } if resp.StatusCode != http.StatusOK { return "", errors.New(resp.Status) } data, err := ioutil.ReadAll(resp.Body) if err != nil { return "", err } return string(data), nil } func main() { data, err := taobao("220.181.111.86") if err != nil { panic(err) } fmt.Println(data) }