Skip to content

Instantly share code, notes, and snippets.

@takatoshiono
Created September 1, 2016 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takatoshiono/2825d78e706d15d451abd8e7621b1eb0 to your computer and use it in GitHub Desktop.
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
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