当前位置: 首页 > backend >正文

go语言实现IP归属地查询

效果:

实现代码main.go

package mainimport ("encoding/json""fmt""io/ioutil""net/http""os"
)type AreaData struct {Continent string `json:"continent"`Country   string `json:"country"`ZipCode   string `json:"zipcode"`Owner     string `json:"owner"`Isp       string `json:"isp"`Adcode    string `json:"adcode"`Prov      string `json:"prov"`City      string `json:"city"`District  string `json:"district"`
}type IpData struct {Code string   `json:"code"`Data AreaData `json:"data"`Ip   string   `json:"ip"`
}func main() {var args = os.Argsif len(args) > 1 {for index, val := range args {if index > 0 {fmt.Printf("parsms[%d]: %s\n", index, val)}}} else {fmt.Println("no args ip address to check exp: ./checkip 14.23.36.151")return}//ip := "14.23.36.151"url := fmt.Sprintf("https://qifu-api.baidubce.com/ip/geo/v1/district?ip=%s", args[1])resp, err := http.Get(url)if err != nil {fmt.Printf("Error making request: %v\n", err)return}defer resp.Body.Close()body, err := ioutil.ReadAll(resp.Body)if err != nil {fmt.Printf("Error reading response body: %v\n", err)return}var ipInfo IpDataerr = json.Unmarshal(body, &ipInfo)if err != nil {fmt.Printf("Error parsing JSON: %v\n", err)return}fmt.Printf("Code: %s\n", ipInfo.Code)fmt.Printf("Ip: %s\n", ipInfo.Ip)fmt.Printf("Continent: %s\n", ipInfo.Data.Continent)fmt.Printf("Country: %s\n", ipInfo.Data.Country)fmt.Printf("ZipCode: %s\n", ipInfo.Data.ZipCode)fmt.Printf("Owner: %s\n", ipInfo.Data.Owner)fmt.Printf("Isp: %s\n", ipInfo.Data.Isp)fmt.Printf("Adcode: %s\n", ipInfo.Data.Adcode)fmt.Printf("Prov: %s\n", ipInfo.Data.Prov)fmt.Printf("City: %s\n", ipInfo.Data.City)fmt.Printf("District: %s\n", ipInfo.Data.District)
}

http://www.xdnf.cn/news/5561.html

相关文章:

  • C++11详解
  • Java 并发编程挑战:从原理到实战的深度剖析与解决方案
  • Go语言即时通讯系统 开发日志day1
  • 扩展:React 项目执行 yarn eject 后的 scripts 目录结构详解
  • LeetCode 2094.找出 3 位偶数:遍历3位偶数
  • ExcelJS库的使用
  • 【技巧】使用frpc点对点安全地内网穿透访问ollama服务
  • 电池串联和并联的区别
  • 《数据结构初阶》【堆 + 堆排序 + TOP-K】
  • 组合问题(二叉树,递归,回溯算法)
  • 48.辐射发射RE和传导发射CE测试方法分析
  • 利用仓颉语言实现一个正整数中数字出现的频次统计
  • 【洛谷P3386】二分图最大匹配之Kuhn算法/匈牙利算法:直观理解
  • AI知识点 | 大模型技术演变
  • 细说getOutputStream()方法
  • 代码随想录笔记---回溯篇
  • libcurl简单使用
  • SpringBoot 整合 Langchain4j 构建AI智能体应用
  • 《异常链机制详解:如何优雅地传递Java中的错误信息?》
  • 【RP2350】香瓜树莓派RP2350之USB虚拟串口
  • windows下安装python软件
  • Linux计划任务与进程
  • 【RP2350】香瓜树莓派RP2350之LED
  • 数字孪生概念
  • 本机的驱动
  • RoPE(旋转位置编码,参考:DeepSeek-V2)
  • Linux进程9-无名管道:1.概述、创建、读写数据、2.进程间通信、3.读写规律、4.fcntl设置阻塞、5.文件描述符概述及复制函数dup,dup2
  • Robot之VideoMimic:《Visual Imitation Enables Contextual Humanoid Control》翻译与解读
  • 安卓系统APP:志愿填报(基于Android平台的志愿填报程序)
  • LVGL环形加载器