This is a creation in Article, where the information may have evolved or changed.
This is the most difficult go demo example of the current study
*****************************************
If I could read it, you'd be apprenticeship, and my mission would be over.
*****************************************
Package Main
Import (
"FMT"
)
Type Fetcher Interface {
Fetch (URL string) (body string, URLs []string, err Error)
}
Func Crawl (URL string, depth int, fetcher fetcher) {
If depth <= 0 {
Return
}
Body, URLs, err: = Fetcher. Fetch (URL)
If err! = Nil {
Fmt. PRINTLN (ERR)
Return
}
Fmt. Printf ("Found:%s%q\n", URL, body)
For _, U: = Range URLs {
Crawl (U, depth-1, fetcher)
}
Return
}
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)
}
var fetcher = fakefetcher{
"http://golang.org/": &fakeresult{
"The Go programming Language",
[]string{
"http://golang.org/pkg/",
"Http://golang.org/cmd/",
},
},
"http://golang.org/pkg/": &fakeresult{
"Packages",
[]string{
"http://golang.org/",
"Http://golang.org/cmd/",
"Http://golang.org/pkg/fmt/",
"Http://golang.org/pkg/os/",
},
},
"http://golang.org/pkg/fmt/": &fakeresult{
"Package Fmt",
[]string{
"http://golang.org/",
"http://golang.org/pkg/",
},
},
"http://golang.org/pkg/os/": &fakeresult{
"Package OS",
[]string{
"http://golang.org/",
"http://golang.org/pkg/",
},
},
}
Func Main () {
Crawl ("http://golang.org/", 4, Fetcher)
}
Finally:
Go Basic Learning is here, later, if the project, I will come here to the actual project problems to say
Thank you, watch this series
About the basic language of go, that's not the responsibility of this series. Please refer to the other tutorials. However, if you want to see the demo of the Go language, I think that I write these more than those in the tutorial, of course, it will have to wait for you to go back to see the experience of the language:)
Remember, a bad demo will make you learn not to go language!