Skip to content

Instantly share code, notes, and snippets.

@c-yan
Created January 18, 2018 22:03
Show Gist options
  • Save c-yan/ed3926925b46ed634ceb4980be8511db to your computer and use it in GitHub Desktop.
Save c-yan/ed3926925b46ed634ceb4980be8511db to your computer and use it in GitHub Desktop.
[A Tour of Go] Exercise: Stringers
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