Created
January 18, 2018 22:03
-
-
Save c-yan/ed3926925b46ed634ceb4980be8511db to your computer and use it in GitHub Desktop.
[A Tour of Go] Exercise: Stringers
This file contains 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" | |
import "strings" | |
type IPAddr [4]byte | |
func (a IPAddr) String() string { | |
tmp := make([]string, 4, 4) | |
for i, v := range a { | |
tmp[i] = int(v).String() | |
} | |
return strings.Join(tmp, ".") | |
} | |
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