Golang 获取当前外网IP/地址/运营商


本文摘自网络,作者,侵删。

通过 Golang 获取当前外网IP、地址、运营商

代码

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
)

func main() {

    responseClient, errClient := http.Get("http://ip.dhcp.cn/?ip") // 获取外网 IP
    if errClient != nil {
        fmt.Printf("获取外网 IP 失败,请检查网络\n")
        panic(errClient)
    }
    // 程序在使用完 response 后必须关闭 response 的主体。
    defer responseClient.Body.Close()

    body, _ := ioutil.ReadAll(responseClient.Body)
    clientIP := fmt.Sprintf("%s", string(body))
    print(clientIP)

}
 $ go run get_ip.go
 134.175.159.160

本文来自:简书

感谢作者:k8s_istio

查看原文:Golang 获取当前外网IP/地址/运营商

相关阅读 >>

Golang 语言怎么使用 Gomail 库发送邮件?

快看!Go-carbon 1.2.2 版本发布了!新增了时间设置和时间差比较功能

Go语言循环语句

Gocn酷Go推荐】Goroutine 泄漏防治神器 Goleak

手撸Golang Go与微服务 聚合模式之1

Golang开发用什么ide

Go语言入门:第一个 Go 程序

Go-grpc-rest环境搭建

Go mod特定依赖包处理

手撸Golang Go与微服务 net.rpc之1

更多相关阅读请进入《Go》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...