1 Star 1 Fork 1

up-zero/gotool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ip.go 664 Bytes
一键复制 编辑 原始数据 按行查看 历史
GetcharZp 提交于 2023-08-28 11:05 +08:00 . feat ipv4
package gotool
import "net"
// Ipv4sLocal 获取本地ipv4地址
func Ipv4sLocal() ([]string, error) {
res := make([]string, 0)
// 获取所有网卡地址
address, err := net.InterfaceAddrs()
if err != nil {
return nil, err
}
// 遍历网卡地址
for _, addr := range address {
// 获取ip地址
ip := addr.(*net.IPNet).IP
// 过滤ip地址
// 127.0.0.0 - 127.255.255.255
// 169.254.0.0 - 169.254.255.255
// 169.254.0.0 - 169.254.255.255
if ip.IsLoopback() || ip.IsLinkLocalMulticast() || ip.IsLinkLocalUnicast() {
continue
}
// 过滤ipv6地址
if ip.To4() != nil {
res = append(res, ip.String())
}
}
return res, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/up-zero/gotool.git
git@gitee.com:up-zero/gotool.git
up-zero
gotool
gotool
main

搜索帮助