Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A Tour of Go: Exercise: Stringers, https://go-tour-jp.appspot.com/methods/18
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