参考:Go语言中文网
文章目录
1、net包
1、lookUp地址信息查找相关
//InterfaceAddrs 返回该系统的网络接口的地址列表。
addr, _ := net.InterfaceAddrs()
fmt.Println(addr)
//Interfaces 返回该系统的网络接口列表
interfaces, _ := net.Interfaces()
fmt.Println(interfaces)
//LookupAddr 查询某个地址,返回映射到该地址的主机名序列
lt, _ := net.LookupAddr("www.alibaba.com")
fmt.Println(lt)
//LookupCNAME函数查询name的规范DNS名(但该域名未必可以访问)。
cname, _ := net.LookupCNAME("www.baidu.com")
fmt.Println(cname)
//LookupHost函数查询主机的网络地址序列。
host, _ := net.LookupHost("www.baidu.com")
fmt.Println(host)
//LookupIP函数查询主机的ipv4和ipv6地址序列。
ip, _ := net.LookupIP("www.baidu.com")
fmt.Println(ip)
2、地址操作
//函数将host和port合并为一个网络地址。一般格式为"host:port";如果host含有冒号或百分号,格式为"[host]:port"。
//Ipv6的文字地址或者主机名必须用方括号括起来,如"[::1]:80"、"[ipv6-host]:http"、"[ipv6-host%zone]:80"。
hp := net.JoinHostPort("127.0.0.1", "8080")
fmt.Println(hp)
//函数将格式为"host:port"、"[host]:port"或"[ipv6-host%zone]:port"的网络地址分割为host或ipv6-host%zone和port两个部分。
shp,port,_ := net.SplitHostPort("127.0.0.1:8080")
fmt.Println(shp," _ ",port)
3、错误说明
接口定义:
type Error interface {
error
Timeout() bool // 错误是否为超时?
Temporary() bool // 错误是否是临时的?
}
错误说明:
读取主机DNS配置时出现的错误。
// DNSError represents a DNS lookup error.
type DNSError struct {
Err string // description of the error
Name string