exercise: interfaces - stringers

This commit is contained in:
Aliberk Sandıkçı 2023-07-24 14:40:34 +03:00
parent 1597f6539d
commit 902591ab43
Signed by: asandikci
GPG key ID: 25C67A03B5666BC1

19
exercises/stringers.go Normal file
View file

@ -0,0 +1,19 @@
package main
import "fmt"
type IPAddr [4]byte
func (i IPAddr) String() string {
return fmt.Sprintf("\"%d.%d.%d.%d\"", i[0], i[1], i[2], i[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)
}
}