Implementation of the exercise: Stringers in “A Tour of Go”

Kasun Siyambalapitiya
1 min readAug 14, 2017

--

When you begin to learn the trending Go lang you may have encounter with the Stringer exercise to print IP addresses in dotted format. Here is how I implemented it.

package mainimport "fmt"type IPAddr [4]byte// if we implement this , this will get called when printing //IPAddr
func (ip IPAddr) String() string{
return fmt.Sprintf("%v.%v.%v.%v",ip[0],ip[1],ip[2],ip[3])
}
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)
}
}

--

--

Kasun Siyambalapitiya
Kasun Siyambalapitiya

No responses yet