zoukankan      html  css  js  c++  java
  • 获取本机ip

    func GetLocalIP() {
        host, _ := os.Hostname()
        addrs, _ := net.LookupIP(host)
        for _, addr := range addrs {
            if ipv4 := addr.To4(); ipv4 != nil {
                fmt.Println("IPv4: ", ipv4)
            }
        }
        fmt.Println("----------------------------------------------------")
        ifaces, err := net.Interfaces()
        if err != nil {
            return
        }
        // handle err
        for _, i := range ifaces {
            addrs, err := i.Addrs()
            if err != nil {
                continue
            }
            // handle err
            for _, addr := range addrs {
                var ip net.IP
                switch v := addr.(type) {
                case *net.IPNet:
                    {
                        ip = v.IP
                        fmt.Println(ip.String())
                    }

                case *net.IPAddr:
                    {
                        ip = v.IP
                        fmt.Println(ip.String())
                    }

                }
                // process IP address
            }
        }
    }
  • 相关阅读:
    Fault-Tolerant Virtual Machine 论文笔记
    Google File System 论文笔记
    Amazon Aurora 论文笔记
    MATLAB入门学习(二):分支语句和编程设计
    MATLAB入门学习(一):基础准备
    矩阵连乘问题
    合并排序 java
    生产者与消费者 代码实现 java
    图的封装(C++)
    二叉树的封装
  • 原文地址:https://www.cnblogs.com/zhangboyu/p/7452817.html
Copyright © 2011-2022 走看看