Created
November 16, 2019 21:20
-
-
Save shalomb/7082152ab18238962011db20ab634ed1 to your computer and use it in GitHub Desktop.
stringer.go https://tour.golang.org/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
// Solution to exercise at https://tour.golang.org/methods/18 | |
// Exercise: Stringers | |
// Make the IPAddr type implement fmt.Stringer to print the address as a dotted quad. | |
// For instance, IPAddr{1, 2, 3, 4} should print as "1.2.3.4". | |
package main | |
import ( | |
"fmt" | |
"strings" | |
"strconv" | |
) | |
type IPAddr [4]byte | |
// TODO: Add a "String() string" method to IPAddr. | |
func (ipv4 IPAddr) String() string { | |
var dq []string | |
for _,b := range ipv4 { | |
dq = append(f, strconv.Itoa(int(b))) | |
} | |
return strings.Join(dq, ".") | |
} | |
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