Exercise: Stringers

Make the IPAddr type implement fmt.Stringer to print the address as a dotted quad.

For instance, IPAddr{1, 2, 3, 4} should print as "1.2.3.4".

exercise-stringer.go

  1. package main
  2. import "fmt"
  3. type IPAddr [4]byte
  4. // TODO: Add a "String() string" method to IPAddr.
  5. func main() {
  6. hosts := map[string]IPAddr{
  7. "loopback": {127, 0, 0, 1},
  8. "googleDNS": {8, 8, 8, 8},
  9. }
  10. for name, ip := range hosts {
  11. fmt.Printf("%v: %v\n", name, ip)
  12. }
  13. }