I am working through the golang tour and I am stuck in one of the exercises. I am not sure why the following does not work for a String() function:
type IPAddr [4]byte
func (addr IPAddr) String() string {
return string(addr[0]) + "." + string(addr[1]) + "." + string(addr[2]) + "." + string(addr[3])
}
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, a)
}
}
Output:
loopback: ...
googleDNS: ...
Granted that using fmt.Sprintf()
would be a nicer solution, but I'm not sure I understand why that function doesn't work.