exercise: interfaces - stringers
This commit is contained in:
parent
1597f6539d
commit
902591ab43
1 changed files with 19 additions and 0 deletions
19
exercises/stringers.go
Normal file
19
exercises/stringers.go
Normal 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)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue