{"id":442231,"date":"2021-04-19T06:44:12","date_gmt":"2021-04-18T22:44:12","guid":{"rendered":"http:\/\/4563.org\/?p=442231"},"modified":"2021-04-19T06:44:12","modified_gmt":"2021-04-18T22:44:12","slug":"%e6%b1%82%e9%97%ae%e3%80%8ago-%e8%af%ad%e8%a8%80%e5%9c%a3%e7%bb%8f%e3%80%8b%e9%87%8c%e8%bf%99%e5%8f%a5%e8%af%9d%e5%ba%94%e8%af%a5%e6%80%8e%e4%b9%88%e8%a7%a3%e9%87%8a%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=442231","title":{"rendered":"\u6c42\u95ee\u300aGO \u8bed\u8a00\u5723\u7ecf\u300b\u91cc\u8fd9\u53e5\u8bdd\u5e94\u8be5\u600e\u4e48\u89e3\u91ca\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u6c42\u95ee\u300aGO \u8bed\u8a00\u5723\u7ecf\u300b\u91cc\u8fd9\u53e5\u8bdd\u5e94\u8be5\u600e\u4e48\u89e3\u91ca\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yuantingwuji <\/span>  <span><i><\/i> 12<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\u6765\u81ea\u300a GO \u8bed\u8a00\u5723\u7ecf\u300b 8.6. \u793a\u4f8b: \u5e76\u53d1\u7684 Web \u722c\u866b<\/p>\n<pre><code>func main() {     worklist := make(chan []string)  \/\/ lists of URLs, may have duplicates     unseenLinks := make(chan string) \/\/ de-duplicated URLs      \/\/ Add command-line arguments to worklist.     go func() { worklist &lt;- os.Args[1:] }()      \/\/ Create 20 crawler goroutines to fetch each unseen link.     for i := 0; i &lt; 20; i++ {         go func() {             for link := range unseenLinks {                 foundLinks := crawl(link)                 go func() { worklist &lt;- foundLinks }() \/\/\u907f\u514d\u6b7b\u9501             }         }()     }      \/\/ The main goroutine de-duplicates worklist items     \/\/ and sends the unseen ones to the crawlers.     seen := make(map[string]bool)     for list := range worklist {         for _, link := range list {             if !seen[link] {                 seen[link] = true                 unseenLinks &lt;- link             }         }     } } <\/code><\/pre>\n<p>\u4e66\u91cc\u9762\u8bf4\uff0c\u201ccrawl \u51fd\u6570\u722c\u5230\u7684\u94fe\u63a5\u5728\u4e00\u4e2a\u4e13\u6709\u7684 goroutine \u4e2d\u88ab\u53d1\u9001\u5230 worklist \u4e2d\u6765\u907f\u514d\u6b7b\u9501\u201d\uff0c\u5c31\u662f\u4e0a\u9762\u4ee3\u7801\u4e2d\u52a0\u4e86\u4e2d\u6587\u6ce8\u91ca\u90a3\u4e00\u884c\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u628a worklist &lt;- foundLinks \u8fd9\u4e2a\u64cd\u4f5c\u52a0\u5165\u4e13\u6709\u534f\u7a0b\u5c31\u6709\u5f15\u8d77\u6b7b\u9501\u5462\uff1f<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>10<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5640454\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : iceheart <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5c31\u662f\u5355\u4e00\u534f\u7a0b\u7a0b\u670d\u52a1\u7684\u6a21\u5f0f\u4ee3\u66ff\u4e34\u754c\u533a\u8bbf\u95ee\u7684\u6a21\u5f0f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5640455\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ihipop <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e00\u8bf4\u7684\u9ad8\u5927\u4e0a\uff0c\u5355\u7eaf\u4ece\u4ee3\u7801\u770b\uff0c\u4e2d\u6587\u6ce8\u91ca\u5904\u8fd9\u662f\u628a\u7ed3\u679c\u53d1\u9001\u7ed9 work list\uff0c\u4f46\u662f worklist \u53ef\u80fd\u662f\u6ee1\u7684\uff0c\u5982\u679c\u8bfb\u53d6 work list \u7684\u90a3\u8fb9\u8bfb\u53d6\u6162\u6216\u8005\u4e0d\u8bfb\u53d6\uff0c\u722c\u53d6\u534f\u7a0b\u5c31\u88ab\u963b\u585e\u5728\u5199\u5165 work list chan \u90a3\u8fb9\u4e86\uff0c\u628a\u53d1\u9001\u884c\u4e3a\u653e\u5230\u5b50\u534f\u7a0b\u5c31\u4e0d\u5f71\u54cd\u722c\u53d6\u534f\u7a0b\u7ee7\u7eed\u722c\u53d6\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5640456\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ihipop <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @ihipop \u7136\u540e<br \/>main \u51fd\u6570\u672b\u5c3e\u4f1a\u628a\u6240\u6709\u672a\u722c\u53d6\u8fc7\u7684\u8fde\u63a5\u8fc7\u6ee4\u4e00\u904d\uff0c\u518d\u53d1\u7ed9 unseenlinks\uff0c\u5982\u679c\u76f4\u63a5\u628a foundlinks \u53d1\u73b0\u7684\u94fe\u63a5\u76f4\u63a5\u7ed9 crawl \u722c\u53d6\uff0c\u90a3\u4e48\u8fd9\u91cc\u53ef\u80fd\u5b58\u5728\u91cd\u590d\u7684\u94fe\u63a5\uff0c\u722c\u866b\u5c31\u4f1a\u9677\u5165\u6b7b\u5faa\u73af\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5640457\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : yuantingwuji <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @ihipop \u5662\uff0c\u6211\u597d\u50cf\u660e\u767d\u4e86\uff0c\u5982\u679c worklist &lt;- foundLinks \u64cd\u4f5c\u4e0d\u52a0\u5165\u4e13\u6709\u534f\u7a0b\uff0c\u5219\u4f1a\u963b\u585e unseenLinks \u8fd9\u4e2a\u53d8\u91cf\u7684\u8bfb\u53d6\uff0c\u8fd9\u6837\u4f1a\u8fdb\u4e00\u6b65\u963b\u585e\u4e3b\u534f\u7a0b unseenLinks \u7684\u63a5\u6536\u64cd\u4f5c\uff0c\u8fdb\u800c\u963b\u585e\u4e3b\u534f\u7a0b worklist \u7684\u7684\u8bfb\u53d6\uff0c\u8fd9\u6837\u5c31\u5f62\u6210\u6b7b\u9501\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5640458\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yeqown <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             unseenLinks \u548c worklist \u5728\u963b\u585e\u60c5\u51b5\u4e0b\u4e92\u76f8\u7b49\u5f85\uff0c\u5c31\u4f1a\u51fa\u73b0\u6b7b\u9501                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5640459\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : mogg <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8003\u8651 unseenLinks \u548c worklist \u90fd\u6ee1\u4e86\u7684\u60c5\u51b5\uff0cworklist &lt;- foundLinks, unseenLinks &lt;- link \u90fd\u963b\u585e\uff0c\u8fd9\u65f6\u5019\u5c31\u6b7b\u9501\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5640460\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : treblex <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8dd1\u4e2a\u9898\uff0c\u4e3a\u5565\u63a5\u6536\u547d\u4ee4\u884c\u53c2\u6570\u4e5f\u8981\u5f00\u4e2a\u534f\u7a0b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5640461\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : VincentYe123 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @treblex \u548c\u4e0a\u9762\u7684\u56de\u7b54\u7684\u539f\u56e0\u4e00\u6837                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5640462\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : kkbblzq <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @treblex \u56e0\u4e3a\u662f\u65e0\u7f13\u51b2\u533a\u7684 chan\uff0c\u539f\u56e0\u8fd8\u662f\u548c\u4e0a\u9762\u4e00\u6837                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5640463\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hq136234303 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @ihipop \u8fd9\u6837\u56de\u7167\u6210\u643a\u7a0b\u7684\u65e0\u9650\u4e0a\u6da8                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u6c42\u95ee\u300aGO \u8bed\u8a00\u5723\u7ecf\u300b\u91cc\u8fd9\u53e5\u8bdd\u5e94\u8be5&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/442231"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=442231"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/442231\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=442231"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=442231"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=442231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}