Skip to content

📋 IPInfo 字段总览

IPInfo 结构体的 28 个字段一览,按类别分组。

🎨 一图抵千言

28 个字段分 8 大类别,一眼看清结构全貌。

💡 28 字段的来由

RetrievedAt 是 SDK 自加的时间戳(非 API 返回),用于缓存与新鲜度判断;其余 27 个字段全部对应 ipapi.co 的 JSON 响应键。Hostname 因可能缺失标注 omitempty

全部字段

字段JSON类型类别
IPipstring网络
Networknetworkstring网络
Versionversionstring网络
Citycitystring地理
Regionregionstring地理
RegionCoderegion_codestring地理
Postalpostal*string地理
Countrycountrystring国家
CountryNamecountry_namestring国家
CountryCodecountry_codestring国家
CountryCodeISO3country_code_iso3string国家
CountryCapitalcountry_capitalstring国家
CountryTLDcountry_tldstring国家
ContinentCodecontinent_codestring国家
InEUin_eubool国家
Latitudelatitudefloat64坐标
Longitudelongitudefloat64坐标
LatLonglatlongstring坐标
Timezonetimezonestring时间
UTCOffsetutc_offsetstring时间
CountryCallingCodecountry_calling_codestring语言
Languageslanguagesstring语言
Currencycurrencystring货币
CurrencyNamecurrency_namestring货币
CountryAreacountry_areafloat64统计
CountryPopulationcountry_populationint统计
ASNasnstringASN
OrgorgstringASN
Hostnamehostname,omitemptystringASN
RetrievedAt-time.TimeSDK 内部

字段访问

go
info, _ := client.GetIPInfo(ctx, "8.8.8.8", "json")
fmt.Println(info.City)              // 结构体字段
fmt.Println(info.GetPostal())      // 指针字段用安全方法
lat, lon, _ := info.ParseLatLong() // 字符串坐标解析
访问方式适用字段特点
直接 info.City*string 字段(27 个)零值为 "",无法区分"缺失"与"空"
info.GetPostal()Postal*stringnil 时返回 "",安全解引用
info.ParseLatLong()LatLong(string)解析 "lat,lon" 为两个 float64
client.GetField(...)任意单字段原始 string,轻量按需查询

⚠️ Postal 为何是指针?

Postal*string——部分国家没有邮编体系,API 返回时会缺失该键。用指针可区分三种状态:nil(缺失)、""(空字符串)、"94043"(有值)。直接用 string 类型会把"缺失"和"空"都塌缩成 "",丢失信息。

单字段查询

任意字段都可用 GetField 单查:

go
city, _ := client.GetField(ctx, "8.8.8.8", "city")
🔍 全量 vs 单字段:何时用哪个?
  • GetIPInfo(全量):需要多个字段、做综合判断(如定位+时区+货币)。一次请求拿全 27 键,省往返。
  • GetField(单字段):只需一个字段(如只看 city 做城市统计)。响应体更小,更快。
  • GetField 返回原始 string:不带类型转换,调用方自行 strconv.ParseFloat 等。

类别导航

下一步

基于 MIT 许可证发布