Created
January 22, 2017 04:11
-
-
Save ta1kt0me/299a48b68eb569f2e5db2ab247ff541c to your computer and use it in GitHub Desktop.
Exercise: Stringers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "strconv" | |
| ) | |
| type IPAddr [4]byte | |
| func (ip IPAddr) String() string { | |
| var str string | |
| for i, v := range ip { | |
| if i != 0 { | |
| str += "." | |
| } | |
| str += strconv.Itoa(int(v)) | |
| } | |
| return fmt.Sprintf("%v", str) | |
| } | |
| 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) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment