{"id":10006981,"date":"2022-12-16T18:02:47","date_gmt":"2022-12-16T10:02:47","guid":{"rendered":"https:\/\/ityet.com\/?p=10006981"},"modified":"2022-12-16T18:02:47","modified_gmt":"2022-12-16T10:02:47","slug":"%e5%b8%a6%e8%af%bb-%e3%80%8ago-in-action%e3%80%8b%e4%b8%ad%e6%96%87%ef%bc%9ago%e8%af%ad%e8%a8%80%e5%ae%9e%e6%88%98-%e4%b8%89","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006981","title":{"rendered":"\u5e26\u8bfb |\u300aGo in Action\u300b(\u4e2d\u6587\uff1aGo\u8bed\u8a00\u5b9e\u6218) (\u4e09)"},"content":{"rendered":"<h2>init<\/h2>\n<h3>init\u5185\u5bb9<\/h3>\n<p>\u672c\u4e66\u63a8\u8350\u5c06map\u6620\u5c04\uff08\u6216\u8005\u53eb\u6ce8\u518c\uff09\u4e4b\u7c7b\u7684\u51c6\u5907\u5de5\u4f5c\u653e\u5728init\u91cc\u9762\uff0cGolang\u4f1a\u4fdd\u8bc1init\u4e2d\u7684\u51fd\u6570\u5728main\u51fd\u6570\u8c03\u7528\u4e4b\u524d\u6267\u884c\u5b8c\u6bd5\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"go\">\/\/ init registers the default matcher with the program.\nfunc init() {\n    var matcher defaultMatcher\n    Register(&quot;default&quot;, matcher)\n}\n\n\/\/ Register is called to register a matcher for use by the program.\nfunc Register(feedType string, matcher Matcher) {\n    if _, exists := matchers[feedType]; exists {\n        log.Fatalln(feedType, &quot;Matcher already registered&quot;)\n    }\n\n    log.Println(&quot;Register&quot;, feedType, &quot;matcher&quot;)\n    matchers[feedType] = matcher\n}<\/code><\/pre>\n<h3>\u5982\u4f55\u4f7f\u7528init<\/h3>\n<p>\u6211\u4eec\u4f7f\u7528\u4e0b\u5212\u7ebf\u6807\u8bc6\u7b26\u4f5c\u4e3a\u522b\u540d\u5bfc\u5165\u5305\uff0c\u5b8c\u6210\u4e86\u8fd9\u4e2a\u8c03\u7528\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8ba9\u7f16\u8bd1\u5668\u5728\u5bfc\u5165\u672a\u88ab\u5f15\u7528\u7684\u5305\u65f6\u4e0d\u62a5\u9519\uff0c\u800c\u4e14\u4f9d\u65e7\u4f1a\u5b9a\u4f4d\u5230\u5305\u5185\u7684init\u51fd\u6570\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"go\">import (\n    _ &quot;December15\/sample\/matchers&quot;\n    &quot;December15\/sample\/search&quot;\n)<\/code><\/pre>\n<h2>\u7ed3\u6784\u7c7b\u578b<\/h2>\n<p>golang\u4e2d\u6ca1\u6709\u7c7b\u7684\u6982\u5ff5\uff0c\u8fd9\u91cc\u7684\u7ed3\u6784\u7c7b\u578b\u7684\u4f5c\u7528\u5c31\u76f8\u5f53\u4e8ejava\u91cc\u9762\u7684\u7c7b\uff0c\u4f8b\u5982\u8fd9\u91cc\u5b9a\u4e49\u4e864\u4e2a\u7c7b\uff1a<\/p>\n<pre><code class=\"go\">type (\n    \/\/ item defines the fields associated with the item tag\n    \/\/ in the rss document.\n    item struct {\n        XMLName     xml.Name `xml:&quot;item&quot;`\n        PubDate     string   `xml:&quot;pubDate&quot;`\n        Title       string   `xml:&quot;title&quot;`\n        Description string   `xml:&quot;description&quot;`\n        Link        string   `xml:&quot;link&quot;`\n        GUID        string   `xml:&quot;guid&quot;`\n        GeoRssPoint string   `xml:&quot;georss:point&quot;`\n    }\n\n    \/\/ image defines the fields associated with the image tag\n    \/\/ in the rss document.\n    image struct {\n        XMLName xml.Name `xml:&quot;image&quot;`\n        URL     string   `xml:&quot;url&quot;`\n        Title   string   `xml:&quot;title&quot;`\n        Link    string   `xml:&quot;link&quot;`\n    }\n\n    \/\/ channel defines the fields associated with the channel tag\n    \/\/ in the rss document.\n    channel struct {\n        XMLName        xml.Name `xml:&quot;channel&quot;`\n        Title          string   `xml:&quot;title&quot;`\n        Description    string   `xml:&quot;description&quot;`\n        Link           string   `xml:&quot;link&quot;`\n        PubDate        string   `xml:&quot;pubDate&quot;`\n        LastBuildDate  string   `xml:&quot;lastBuildDate&quot;`\n        TTL            string   `xml:&quot;ttl&quot;`\n        Language       string   `xml:&quot;language&quot;`\n        ManagingEditor string   `xml:&quot;managingEditor&quot;`\n        WebMaster      string   `xml:&quot;webMaster&quot;`\n        Image          image    `xml:&quot;image&quot;`\n        Item           []item   `xml:&quot;item&quot;`\n    }\n\n    \/\/ rssDocument defines the fields associated with the rss document.\n    rssDocument struct {\n        XMLName xml.Name `xml:&quot;rss&quot;`\n        Channel channel  `xml:&quot;channel&quot;`\n    }\n)\n<\/code><\/pre>\n<h2>\u7a7a\u7ed3\u6784\u5b9e\u73b0\u63a5\u53e3<\/h2>\n<p>\u672c\u4e66\u63a8\u8350\u5728\u4e0d\u9700\u8981\u7ef4\u62a4\u4efb\u4f55\u72b6\u6001\u7684\u65f6\u5019\uff0c\u7528\u7a7a\u7ed3\u6784\u4f53\u6765\u5b9e\u73b0\u63a5\u53e3\uff0c\u6bd4\u5982\uff1a<\/p>\n<pre><code class=\"go\">cxtype rssMatcher struct{}\n\nfunc (m rssMatcher) Search(feed *search.Feed, searchTerm string) ([]*search.Result, error) {\n    ...\n}\n<\/code><\/pre>\n<h2>\u7f51\u7edc\u8bf7\u6c42<\/h2>\n<p>Golang\u53ef\u4ee5\u975e\u5e38\u5206\u65b9\u4fbf\u7684\u8fdb\u884c\u7f51\u7edc\u8bf7\u6c42\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"go\">\/\/ retrieve performs a HTTP Get request for the rss feed and decodes the results.\nfunc (m rssMatcher) retrieve(feed *search.Feed) (*rssDocument, error) {\n    if feed.URI == &quot;&quot; {\n        return nil, errors.New(&quot;No rss feed uri provided&quot;)\n    }\n\n    \/\/ Retrieve the rss feed document from the web.\n    resp, err := http.Get(feed.URI)\n    if err != nil {\n        return nil, err\n    }\n\n    \/\/ Close the response once we return from the function.\n    defer resp.Body.Close()\n\n    \/\/ Check the status code for a 200 so we know we have received a\n    \/\/ proper response.\n    if resp.StatusCode != 200 {\n        return nil, fmt.Errorf(&quot;HTTP Response Error %d\/n&quot;, resp.StatusCode)\n    }\n\n    \/\/ Decode the rss feed document into our struct type.\n    \/\/ We don't need to check for errors, the caller can do this.\n    var document rssDocument\n    err = xml.NewDecoder(resp.Body).Decode(&amp;document)\n    return &amp;document, err\n}<\/code><\/pre>\n<hr>\n<blockquote><p>\u53c2\u8003\uff1aKennedy W ,  Ketelsen B ,  Martin E S . Go in action.  2016.<\/p><\/blockquote>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc4VJk\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000043098779\">https:\/\/segmentfault.com\/a\/1190000043098779<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>init init\u5185\u5bb9 \u672c\u4e66\u63a8\u8350\u5c06map\u6620\u5c04\uff08\u6216\u8005\u53eb\u6ce8\u518c\uff09\u4e4b\u7c7b\u7684\u51c6\u5907\u5de5\u4f5c\u653e\u5728init\u91cc\u9762\uff0cGolang\u4f1a\u4fdd\u8bc1i [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10006981","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006981","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006981"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006981\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}