package geo import ( "encoding/json" "github.com/pkg/errors" "net/http" ) func Ipapi() (prov, city, isp string, err error) { const endpoint = "http://ip-api.com/json/?fields=regionName,city,as&lang=zh-CN" resp, err := http.Get(endpoint) if err != nil { return "", "", "", err } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return "", "", "", err } var data struct { RegionName string `json:"regionName"` City string `json:"city"` As string `json:"as"` } err = json.NewDecoder(resp.Body).Decode(&data) if err != nil { return "", "", "", err } prov = data.RegionName city = data.City isp = data.As if prov == "" || city == "" || isp == "" { return "", "", "", errors.New("解析数据为空") } return prov, city, isp, nil }