{"id":1253,"date":"2022-12-06T08:40:00","date_gmt":"2022-12-06T00:40:00","guid":{"rendered":"https:\/\/fdream.net\/blog\/?p=1253"},"modified":"2022-12-06T09:26:29","modified_gmt":"2022-12-06T01:26:29","slug":"go%e6%8c%87%e5%8d%97%e7%bb%83%e4%b9%a011-web%e7%88%ac%e8%99%ab","status":"publish","type":"post","link":"https:\/\/fdream.net\/blog\/article\/1253","title":{"rendered":"Go\u6307\u5357\u7ec3\u4e6011-Web\u722c\u866b"},"content":{"rendered":"\n<p>\u8fd9\u4e2a\u662f<a rel=\"noreferrer noopener\" href=\"https:\/\/go.dev\/tour\/list\" target=\"_blank\">A Tour of Go<\/a>\u8fd9\u4e2a\u6559\u7a0b\u91cc\u9762\u7684\u6700\u540e\u4e00\u4e2a\u7ec3\u4e60\uff0c<a rel=\"noreferrer noopener\" href=\"https:\/\/tour.go-zh.org\/concurrency\/8\" target=\"_blank\">\u539f<\/a><a href=\"https:\/\/tour.go-zh.org\/concurrency\/10\" target=\"_blank\" rel=\"noreferrer noopener\">\u9898<\/a>\u5982\u4e0b\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u5728\u8fd9\u4e2a\u7ec3\u4e60\u4e2d\uff0c\u6211\u4eec\u5c06\u4f1a\u4f7f\u7528 Go \u7684\u5e76\u53d1\u7279\u6027\u6765\u5e76\u884c\u5316\u4e00\u4e2a Web \u722c\u866b\u3002<\/p>\n\n\n\n<p>\u4fee\u6539&nbsp;<code>Crawl<\/code>&nbsp;\u51fd\u6570\u6765\u5e76\u884c\u5730\u6293\u53d6 URL\uff0c\u5e76\u4e14\u4fdd\u8bc1\u4e0d\u91cd\u590d\u3002<\/p>\n\n\n\n<p><em>\u63d0\u793a<\/em>\uff1a\u4f60\u53ef\u4ee5\u7528\u4e00\u4e2a map \u6765\u7f13\u5b58\u5df2\u7ecf\u83b7\u53d6\u7684 URL\uff0c\u4f46\u662f\u8981\u6ce8\u610f map \u672c\u8eab\u5e76\u4e0d\u662f\u5e76\u53d1\u5b89\u5168\u7684\uff01<\/p>\n<\/blockquote>\n\n\n\n<p>\u539f\u9898\u4ee3\u7801\u6a21\u677f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n\t\"fmt\"\n)\n\ntype Fetcher interface {\n\t\/\/ Fetch \u8fd4\u56de URL \u7684 body \u5185\u5bb9\uff0c\u5e76\u4e14\u5c06\u5728\u8fd9\u4e2a\u9875\u9762\u4e0a\u627e\u5230\u7684 URL \u653e\u5230\u4e00\u4e2a slice \u4e2d\u3002\n\tFetch(url string) (body string, urls &#91;]string, err error)\n}\n\n\/\/ Crawl \u4f7f\u7528 fetcher \u4ece\u67d0\u4e2a URL \u5f00\u59cb\u9012\u5f52\u7684\u722c\u53d6\u9875\u9762\uff0c\u76f4\u5230\u8fbe\u5230\u6700\u5927\u6df1\u5ea6\u3002\nfunc Crawl(url string, depth int, fetcher Fetcher) {\n\t\/\/ TODO: \u5e76\u884c\u7684\u6293\u53d6 URL\u3002\n\t\/\/ TODO: \u4e0d\u91cd\u590d\u6293\u53d6\u9875\u9762\u3002\n\t\/\/ \u4e0b\u9762\u5e76\u6ca1\u6709\u5b9e\u73b0\u4e0a\u9762\u4e24\u79cd\u60c5\u51b5\uff1a\n\tif depth &lt;= 0 {\n\t\treturn\n\t}\n\tbody, urls, err := fetcher.Fetch(url)\n\tif err != nil {\n\t\tfmt.Println(err)\n\t\treturn\n\t}\n\tfmt.Printf(\"found: %s %q\\n\", url, body)\n\tfor _, u := range urls {\n\t\tCrawl(u, depth-1, fetcher)\n\t}\n\treturn\n}\n\nfunc main() {\n\tCrawl(\"https:\/\/golang.org\/\", 4, fetcher)\n}\n\n\/\/ fakeFetcher \u662f\u8fd4\u56de\u82e5\u5e72\u7ed3\u679c\u7684 Fetcher\u3002\ntype fakeFetcher map&#91;string]*fakeResult\n\ntype fakeResult struct {\n\tbody string\n\turls &#91;]string\n}\n\nfunc (f fakeFetcher) Fetch(url string) (string, &#91;]string, error) {\n\tif res, ok := f&#91;url]; ok {\n\t\treturn res.body, res.urls, nil\n\t}\n\treturn \"\", nil, fmt.Errorf(\"not found: %s\", url)\n}\n\n\/\/ fetcher \u662f\u586b\u5145\u540e\u7684 fakeFetcher\u3002\nvar fetcher = fakeFetcher{\n\t\"https:\/\/golang.org\/\": &amp;fakeResult{\n\t\t\"The Go Programming Language\",\n\t\t&#91;]string{\n\t\t\t\"https:\/\/golang.org\/pkg\/\",\n\t\t\t\"https:\/\/golang.org\/cmd\/\",\n\t\t},\n\t},\n\t\"https:\/\/golang.org\/pkg\/\": &amp;fakeResult{\n\t\t\"Packages\",\n\t\t&#91;]string{\n\t\t\t\"https:\/\/golang.org\/\",\n\t\t\t\"https:\/\/golang.org\/cmd\/\",\n\t\t\t\"https:\/\/golang.org\/pkg\/fmt\/\",\n\t\t\t\"https:\/\/golang.org\/pkg\/os\/\",\n\t\t},\n\t},\n\t\"https:\/\/golang.org\/pkg\/fmt\/\": &amp;fakeResult{\n\t\t\"Package fmt\",\n\t\t&#91;]string{\n\t\t\t\"https:\/\/golang.org\/\",\n\t\t\t\"https:\/\/golang.org\/pkg\/\",\n\t\t},\n\t},\n\t\"https:\/\/golang.org\/pkg\/os\/\": &amp;fakeResult{\n\t\t\"Package os\",\n\t\t&#91;]string{\n\t\t\t\"https:\/\/golang.org\/\",\n\t\t\t\"https:\/\/golang.org\/pkg\/\",\n\t\t},\n\t},\n}<\/code><\/pre>\n\n\n\n<p>\u5e76\u53d1\u5b89\u5168\u9700\u8981\u4f7f\u7528\u4e92\u65a5\u9501<code>sync.Mutex<\/code>\u6765\u5904\u7406\uff0c\u53ef\u4ee5\u53c2\u8003\u6559\u7a0b\u524d\u4e00\u9875\u7684\u4f7f\u7528\u65b9\u6cd5\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n\t\"fmt\"\n\t\"sync\"\n)\n\ntype Fetcher interface {\n\t\/\/ Fetch \u8fd4\u56de URL \u7684 body \u5185\u5bb9\uff0c\u5e76\u4e14\u5c06\u5728\u8fd9\u4e2a\u9875\u9762\u4e0a\u627e\u5230\u7684 URL \u653e\u5230\u4e00\u4e2a slice \u4e2d\u3002\n\tFetch(url string) (body string, urls &#91;]string, err error)\n}\n\ntype Cache struct {\n\turls map&#91;string]bool\n\tsync.Mutex\n}\n\nfunc (c *Cache) Add(url string) {\n\tc.Lock()\n\tdefer c.Unlock()\n\tc.urls&#91;url] = true\n}\n\nfunc (c *Cache) Has(url string) bool {\n\tc.Lock()\n\tdefer c.Unlock()\n\t_, ok := c.urls&#91;url]\n\treturn ok\n}\n\nvar cache = Cache{urls: make(map&#91;string]bool)}\n\n\/\/ Crawl \u4f7f\u7528 fetcher \u4ece\u67d0\u4e2a URL \u5f00\u59cb\u9012\u5f52\u7684\u722c\u53d6\u9875\u9762\uff0c\u76f4\u5230\u8fbe\u5230\u6700\u5927\u6df1\u5ea6\u3002\nfunc Crawl(url string, depth int, fetcher Fetcher) {\n\t\/\/ TODO: \u5e76\u884c\u7684\u6293\u53d6 URL\u3002\n\t\/\/ TODO: \u4e0d\u91cd\u590d\u6293\u53d6\u9875\u9762\u3002\n\t\/\/ \u4e0b\u9762\u5e76\u6ca1\u6709\u5b9e\u73b0\u4e0a\u9762\u4e24\u79cd\u60c5\u51b5\uff1a\n\tif depth &lt;= 0 {\n\t\treturn\n\t}\n\t\n\tif cache.Has(url) {\n\t\treturn\n\t}\n\t\n\tcache.Add(url)\n\tbody, urls, err := fetcher.Fetch(url)\n\tif err != nil {\n\t\tfmt.Println(err)\n\t\treturn\n\t}\n\tfmt.Printf(\"found: %s %q\\n\", url, body)\n\tch := make(chan int) \/\/ \u9632\u6b62goroutine\u9000\u51fa\n\tfor _, u := range urls {\n\t\tgo func(url string) {\n\t\t\tCrawl(url, depth-1, fetcher)\n\t\t\tch &lt;- 1\n\t\t}(u)\n\t}\n\tfor range urls {\n\t\t&lt;- ch\n\t}\n\treturn\n}\n\nfunc main() {\n\tCrawl(\"https:\/\/golang.org\/\", 4, fetcher)\n}\n\n\/\/ fakeFetcher \u662f\u8fd4\u56de\u82e5\u5e72\u7ed3\u679c\u7684 Fetcher\u3002\ntype fakeFetcher map&#91;string]*fakeResult\n\ntype fakeResult struct {\n\tbody string\n\turls &#91;]string\n}\n\nfunc (f fakeFetcher) Fetch(url string) (string, &#91;]string, error) {\n\tif res, ok := f&#91;url]; ok {\n\t\treturn res.body, res.urls, nil\n\t}\n\treturn \"\", nil, fmt.Errorf(\"not found: %s\", url)\n}\n\n\/\/ fetcher \u662f\u586b\u5145\u540e\u7684 fakeFetcher\u3002\nvar fetcher = fakeFetcher{\n\t\"https:\/\/golang.org\/\": &amp;fakeResult{\n\t\t\"The Go Programming Language\",\n\t\t&#91;]string{\n\t\t\t\"https:\/\/golang.org\/pkg\/\",\n\t\t\t\"https:\/\/golang.org\/cmd\/\",\n\t\t},\n\t},\n\t\"https:\/\/golang.org\/pkg\/\": &amp;fakeResult{\n\t\t\"Packages\",\n\t\t&#91;]string{\n\t\t\t\"https:\/\/golang.org\/\",\n\t\t\t\"https:\/\/golang.org\/cmd\/\",\n\t\t\t\"https:\/\/golang.org\/pkg\/fmt\/\",\n\t\t\t\"https:\/\/golang.org\/pkg\/os\/\",\n\t\t},\n\t},\n\t\"https:\/\/golang.org\/pkg\/fmt\/\": &amp;fakeResult{\n\t\t\"Package fmt\",\n\t\t&#91;]string{\n\t\t\t\"https:\/\/golang.org\/\",\n\t\t\t\"https:\/\/golang.org\/pkg\/\",\n\t\t},\n\t},\n\t\"https:\/\/golang.org\/pkg\/os\/\": &amp;fakeResult{\n\t\t\"Package os\",\n\t\t&#91;]string{\n\t\t\t\"https:\/\/golang.org\/\",\n\t\t\t\"https:\/\/golang.org\/pkg\/\",\n\t\t},\n\t},\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u4e2a\u662fA Tour of Go\u8fd9\u4e2a\u6559\u7a0b\u91cc\u9762\u7684\u6700\u540e\u4e00\u4e2a\u7ec3\u4e60\uff0c\u539f\u9898\u5982\u4e0b\uff1a \u5728\u8fd9\u4e2a\u7ec3\u4e60\u4e2d\uff0c\u6211\u4eec\u5c06\u4f1a\u4f7f\u7528 Go \u7684\u5e76\u53d1\u7279\u6027\u6765\u5e76\u884c\u5316\u4e00\u4e2a Web \u722c\u866b\u3002 \u4fee\u6539&nbsp;Crawl&nbsp;\u51fd\u6570\u6765\u5e76\u884c\u5730\u6293\u53d6 URL\uff0c\u5e76\u4e14\u4fdd\u8bc1\u4e0d\u91cd\u590d\u3002 \u63d0\u793a\uff1a\u4f60\u53ef\u4ee5\u7528\u4e00\u4e2a map \u6765\u7f13\u5b58\u5df2\u7ecf\u83b7\u53d6\u7684 URL\uff0c\u4f46\u662f\u8981\u6ce8\u610f map \u672c\u8eab\u5e76\u4e0d\u662f\u5e76\u53d1\u5b89\u5168\u7684\uff01 \u539f\u9898\u4ee3\u7801\u6a21\u677f\u5982\u4e0b\uff1a \u5e76\u53d1\u5b89\u5168\u9700\u8981\u4f7f\u7528\u4e92\u65a5\u9501sync.Mutex\u6765\u5904\u7406\uff0c\u53ef\u4ee5\u53c2\u8003\u6559\u7a0b\u524d\u4e00\u9875\u7684\u4f7f\u7528\u65b9\u6cd5\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[374],"class_list":["post-1253","post","type-post","status-publish","format-standard","hentry","category-coding","tag-go"],"views":416,"_links":{"self":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts\/1253","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/comments?post=1253"}],"version-history":[{"count":0,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts\/1253\/revisions"}],"wp:attachment":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/media?parent=1253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/categories?post=1253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/tags?post=1253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}