Skip to content

Instantly share code, notes, and snippets.

@limpido
Last active August 8, 2025 02:26
Show Gist options
  • Save limpido/8e341dbc0b1cb70a21bb2203dc297d51 to your computer and use it in GitHub Desktop.
Save limpido/8e341dbc0b1cb70a21bb2203dc297d51 to your computer and use it in GitHub Desktop.
My solutions to A Tour of Go Exercises
package main
import "golang.org/x/tour/tree"
import "fmt"
// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch chan int) {
walking(t, ch)
close(ch)
}
func walking(t *tree.Tree, ch chan int) {
if t == nil {
return
}
walking(t.Left, ch)
ch <- t.Value
walking(t.Right, ch)
}
// Same determines whether the trees
// t1 and t2 contain the same values.
func Same(t1, t2 *tree.Tree) bool {
c1 := make(chan int)
c2 := make(chan int)
go Walk(t1, c1)
go Walk(t2, c2)
for {
v1, ok1 := <-c1
v2, ok2 := <-c2
if ok1 != ok2 || v1 != v2 {
return false
}
if ok1 == false {
break
}
}
return true
}
func main() {
ch := make(chan int)
go Walk(tree.New(1), ch)
for i := range ch {
fmt.Printf("%d\n", i)
}
fmt.Println(Same(tree.New(1), tree.New(1)))
fmt.Println(Same(tree.New(1), tree.New(2)))
}
package main
import (
"fmt"
"sync"
)
type Fetcher interface {
// Fetch returns the body of URL and
// a slice of URLs found on that page.
Fetch(url string) (body string, urls []string, err error)
}
type Cacher struct {
mu sync.Mutex
m map[string]bool
}
func (c *Cacher) Cache(url string) {
c.mu.Lock()
c.m[url] = true
c.mu.Unlock()
}
func (c *Cacher) Contains(url string) bool {
c.mu.Lock()
defer c.mu.Unlock()
return c.m[url]
}
var cacher Cacher = Cacher{m: make(map[string]bool)}
// Crawl uses fetcher to recursively crawl
// pages starting with url, to a maximum of depth.
func Crawl(url string, depth int, fetcher Fetcher, ch chan bool) {
// TODO: Fetch URLs in parallel.
// TODO: Don't fetch the same URL twice.
// This implementation doesn't do either:
if depth <= 0 {
ch <- true
return
}
if (cacher.Contains(url)) {
ch <- true
return
}
body, urls, err := fetcher.Fetch(url)
if err != nil {
fmt.Println(err)
ch <- true
return
}
fmt.Printf("found: %s %q\n", url, body)
cacher.Cache(url)
c := make(chan bool)
for _, u := range urls {
go Crawl(u, depth-1, fetcher, c)
}
for i:=0; i < len(urls); i++ {
<-c
}
ch <- true
}
func main() {
ch := make(chan bool)
go Crawl("https://golang.org/", 4, fetcher, ch)
<-ch
}
// fakeFetcher is Fetcher that returns canned results.
type fakeFetcher map[string]*fakeResult
type fakeResult struct {
body string
urls []string
}
func (f fakeFetcher) Fetch(url string) (string, []string, error) {
if res, ok := f[url]; ok {
return res.body, res.urls, nil
}
return "", nil, fmt.Errorf("not found: %s", url)
}
// fetcher is a populated fakeFetcher.
var fetcher = fakeFetcher{
"https://golang.org/": &fakeResult{
"The Go Programming Language",
[]string{
"https://golang.org/pkg/",
"https://golang.org/cmd/",
},
},
"https://golang.org/pkg/": &fakeResult{
"Packages",
[]string{
"https://golang.org/",
"https://golang.org/cmd/",
"https://golang.org/pkg/fmt/",
"https://golang.org/pkg/os/",
},
},
"https://golang.org/pkg/fmt/": &fakeResult{
"Package fmt",
[]string{
"https://golang.org/",
"https://golang.org/pkg/",
},
},
"https://golang.org/pkg/os/": &fakeResult{
"Package os",
[]string{
"https://golang.org/",
"https://golang.org/pkg/",
},
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment