I went through a review tour and I completed the Internet search exercise, but I think that the method I used to print all the results may be ineffective.
Here is my code. I just edited the workaround and main features, so I will just post this. Here is a link to the exercise ( http://tour.golang.org/#70 )
var used = make(map[string]bool) func Crawl(url string, depth int, fetcher Fetcher, results chan string) { if depth <= 0 { return } body, urls, err := fetcher.Fetch(url) if err != nil { results <- fmt.Sprintf("%v",err) return } results <-fmt.Sprintf("\nfound: %s %q\n", url, body) for _,u := range urls { if used[u] == false { used[u] = true go Crawl(u, depth-1, fetcher, results) } } return }
I use the line "for i <len (used)" in the main to ensure that the value of the results will be printed only if there is a result to print. I can't just use
for i := range results
because itβs difficult to use βclose (results)β in the traversal function, since it is recursive, but with how I do it, I need to find the length of the variable each time.
Is there a better way to do this?
source share