slice 类型的 ip 地址
type IPAddr [4]byte
字符串型的
"8.8.8.8"
"192.168.1.1"
将第一种转换为第二种
//IPAddr
type IPAddr [4]byte
func (ip IPAddr) String() string {
s := make([]string, len(ip))
for i := range ip {
s[i] = strconv.Itoa(int(ip[i]))
}
return strings.Join(s, ".")
}
func main() {
addrs := map[string]IPAddr{
"loopback": {127, 0, 0, 1},
"googleDNS": {8, 8, 8, 8},
}
for n, a := range addrs {
fmt.Printf("%v: %v\n", n, a)
}
}
···
暂时没有留言