Go Linux Practice 4

Source: Internet
Author: User
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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.