Created
September 1, 2016 22:28
-
-
Save takatoshiono/2825d78e706d15d451abd8e7621b1eb0 to your computer and use it in GitHub Desktop.
A Tour of Go: Exercise: Stringers, https://go-tour-jp.appspot.com/methods/18
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 | |
// Add a "String() string" method to IPAddr. | |
func (ipaddr IPAddr) String() string { | |
octets := make([]string, 4) | |
for i, v := range ipaddr { | |
octets[i] = fmt.Sprintf("%v", v) | |
} | |
return strings.Join(octets, ".") | |
} | |
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