Golang学习之InterfaceAddrs获取本地IP

前端之家收集整理的这篇文章主要介绍了Golang学习之InterfaceAddrs获取本地IP前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Go 获取本地IP

package main
import (
    "fmt"
    "net"
    "os"
)
func main() {
    addrs,err := net.InterfaceAddrs()
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    for _,address := range addrs {
        // 检查ip地址判断是否回环地址
        if ipnet,ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
            if ipnet.IP.To4() != nil {
                fmt.Println(ipnet.IP.String())
            }
        }
    }
}

代码所示

Go语言获取外网和本地IP

package main

import (
    "flag"
    "fmt"
    "io"
    "net"
    "net/http"
    "os"
)

var get_ip = flag.String("get_ip","","external|internal")

func main() {
    fmt.Println("Usage of ./getmyip --get_ip=(external|internal)")
    flag.Parse()
    if *get_ip == "external" {
        get_external()
    }

    if *get_ip == "internal" {
        get_internal()
    }

}

func get_external() {
    resp,err := http.Get("http://myexternalip.com/raw")
    if err != nil {
        os.Stderr.WriteString(err.Error())
        os.Stderr.WriteString("\n")
        os.Exit(1)
    }
    defer resp.Body.Close()
    io.Copy(os.Stdout,resp.Body)
    os.Exit(0)
}

func get_internal() {
    addrs,err := net.InterfaceAddrs()
    if err != nil {
        os.Stderr.WriteString("Oops:" + err.Error())
        os.Exit(1)
    }
    for _,a := range addrs {
        if ipnet,ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
            if ipnet.IP.To4() != nil {
                os.Stdout.WriteString(ipnet.IP.String() + "\n")
            }
        }
    }
    os.Exit(0)
}

猜你在找的Go相关文章