1 Star 2 Fork 1

麻本法 / go.ywloader-cli

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
localaddress.go 641 Bytes
一键复制 编辑 原始数据 按行查看 历史
package simpleserver
import (
"net"
"strings"
)
type AddressType int
const (
IPV4 AddressType = iota
IPV6
)
func GetLocalAddress(t AddressType) ([]string, error) {
var address []string
addrs, err := net.InterfaceAddrs()
if err != nil {
return nil, err
}
for _, addr := range addrs {
// addr.String() -> 127.0.0.1/8
if ipnet, ok := addr.(*net.IPNet); ok {
if t == IPV4 && !strings.Contains(ipnet.IP.String(), "::") {
address = append(address, ipnet.IP.String())
}
if t == IPV6 && strings.Contains(ipnet.IP.String(), "::") {
address = append(address, ipnet.IP.String())
}
}
}
return address, nil
}
Go
1
https://gitee.com/zinface/ywloader-cli.git
git@gitee.com:zinface/ywloader-cli.git
zinface
ywloader-cli
go.ywloader-cli
v1.2.0

搜索帮助

53164aa7 5694891 3bd8fe86 5694891