package main import ( "fmt" "strings" "strconv" ) type IPAddr [4]byte func (ip IPAddr) String() string { s := make([]string, len(ip)) for i, val := range ip { s[i] = strconv.Itoa(int(val)) } return strings.Join(s, ".") } func main() { hosts := map[string]IPAddr{ "loopback": {127, 0, 0, 1}, "googleDNS": {8, 8, 8, 8}, } for name, ip := range hosts { fmt.Printf("%v: %v\n", name, ip) } }
标签:string,val,ip,IPAddr,Stringers,Tour,Go,Exercise From: https://www.cnblogs.com/zhangzhihui/p/17633663.html