Skip to content

Instantly share code, notes, and snippets.

@javiyt
Last active July 9, 2019 13:37
Show Gist options
  • Save javiyt/c6c2dc317328156c558f119c1470af5c to your computer and use it in GitHub Desktop.
Save javiyt/c6c2dc317328156c558f119c1470af5c to your computer and use it in GitHub Desktop.
A Tour of Go: Exercises
package main
import (
"strings"
"golang.org/x/tour/wc"
)
func WordCount(s string) map[string]int {
words := make(map[string]int)
for _, word := range strings.Fields(s) {
var count, _ = words[word]
words[word] = count + 1
}
return words
}
func main() {
wc.Test(WordCount)
}
package main
import (
"fmt"
"strings"
"strconv"
)
type IPAddr [4]byte
// TODO: Add a "String() string" method to IPAddr.
func (ip IPAddr) String() string {
s := make([]string, len(ip))
for i, val := range ip {
s[i] = strconv.Itoa(int(val))
}
return strings.Join(s, ".")
}
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