{"id":309130,"date":"2024-05-20T21:18:14","date_gmt":"2024-05-20T13:18:14","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/309130.html"},"modified":"2024-05-20T21:18:30","modified_gmt":"2024-05-20T13:18:30","slug":"go-%e8%af%ad%e8%a8%80%e7%ad%96%e7%95%a5%e6%a8%a1%e5%bc%8f%e6%80%8e%e4%b9%88%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/309130.html","title":{"rendered":"go \u8bed\u8a00\u7b56\u7565\u6a21\u5f0f\u600e\u4e48\u5b9e\u73b0"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26225602\/dea5c30c-3bad-45bb-9323-e3f7deb70f26.webp\" alt=\"go \u8bed\u8a00\u7b56\u7565\u6a21\u5f0f\u600e\u4e48\u5b9e\u73b0\" \/><\/p>\n<p><p>\u7b56\u7565\u6a21\u5f0f\u662f\u4e00\u79cd\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u5b83\u5141\u8bb8\u5728\u8fd0\u884c\u65f6\u9009\u62e9\u6700\u5408\u9002\u7684\u7b97\u6cd5\u6216\u884c\u4e3a\u3002\u5b83\u5b9a\u4e49\u4e86\u4e00\u7cfb\u5217\u7684\u7b97\u6cd5\uff0c\u5e76\u5c06\u6bcf\u4e2a\u7b97\u6cd5\u5c01\u88c5\u8d77\u6765\uff0c\u4f7f\u5b83\u4eec\u53ef\u4ee5\u76f8\u4e92\u66ff\u6362\u3002<strong>\u5728Go\u8bed\u8a00\u4e2d\u5b9e\u73b0\u7b56\u7565\u6a21\u5f0f\u4e3b\u8981\u6d89\u53ca\u5b9a\u4e49\u7b56\u7565\u63a5\u53e3\u3001\u5b9e\u73b0\u5177\u4f53\u7b56\u7565\u3001\u5b9a\u4e49\u4e0a\u4e0b\u6587\u7ed3\u6784<\/strong>\u7b49\u5173\u952e\u6b65\u9aa4\u3002Go\u8bed\u8a00\u7684\u63a5\u53e3\u7279\u6027\u4f7f\u5f97\u5b9e\u73b0\u7b56\u7565\u6a21\u5f0f\u53d8\u5f97\u975e\u5e38\u81ea\u7136\u548c\u4fbf\u6377\u3002<strong>\u5177\u4f53\u7b56\u7565\u901a\u8fc7\u5b9e\u73b0\u7edf\u4e00\u7684\u63a5\u53e3\u6765\u4fdd\u8bc1\u53ef\u4e92\u6362\u6027<\/strong>\uff0c\u800c\u4e0a\u4e0b\u6587\u5219\u6301\u6709\u7b56\u7565\u63a5\u53e3\u7684\u5f15\u7528\uff0c\u5728\u8fd0\u884c\u65f6\u51b3\u5b9a\u4f7f\u7528\u54ea\u4e2a\u5177\u4f53\u7b56\u7565\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u5b9a\u4e49\u7b56\u7565\u63a5\u53e3<\/h2>\n<\/p>\n<p><p>\u5728Go\u8bed\u8a00\u4e2d\uff0c\u4e00\u4e2a\u7b56\u7565\u6a21\u5f0f\u901a\u5e38\u4ece\u5b9a\u4e49\u4e00\u4e2a\u63a5\u53e3\u5f00\u59cb\u3002\u8fd9\u4e2a\u63a5\u53e3\u58f0\u660e\u4e86\u7b56\u7565\u5fc5\u987b\u5b9e\u73b0\u7684\u65b9\u6cd5\uff0c\u6240\u6709\u7684\u7b56\u7565\u5b9e\u73b0\u90fd\u5fc5\u987b\u9075\u5b88\u8fd9\u4e2a\u63a5\u53e3\u7684\u89c4\u5b9a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-go\">type Strategy interface {<\/p>\n<p>    Execute() error<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u5b9e\u73b0\u5177\u4f53\u7b56\u7565<\/h2>\n<\/p>\n<p><p>\u5177\u4f53\u7b56\u7565\u662f\u5b9e\u73b0\u7b56\u7565\u63a5\u53e3\u7684\u7c7b\u578b\uff0c\u5b83\u4eec\u5177\u4f53\u5316\u4e86\u7b56\u7565\u7684\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-go\">\/\/ \u5177\u4f53\u7b56\u7565A<\/p>\n<p>type StrategyA struct {<\/p>\n<p>    \/\/ ...<\/p>\n<p>}<\/p>\n<p>func (s *StrategyA) Execute() error {<\/p>\n<p>    \/\/ \u5177\u4f53\u7684\u6267\u884c\u4ee3\u7801<\/p>\n<p>    return nil<\/p>\n<p>}<\/p>\n<p>\/\/ \u5177\u4f53\u7b56\u7565B<\/p>\n<p>type StrategyB struct {<\/p>\n<p>    \/\/ ...<\/p>\n<p>}<\/p>\n<p>func (s *StrategyB) Execute() error {<\/p>\n<p>    \/\/ \u5177\u4f53\u7684\u6267\u884c\u4ee3\u7801<\/p>\n<p>    return nil<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5b9e\u73b0<code>Strategy<\/code>\u63a5\u53e3\u7684<code>Execute<\/code>\u65b9\u6cd5\uff0c<code>StrategyA<\/code>\u548c<code>StrategyB<\/code>\u6210\u4e3a\u4e86\u5177\u4f53\u7684\u7b56\u7565\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u5b9a\u4e49\u4e0a\u4e0b\u6587\u7ed3\u6784<\/h2>\n<\/p>\n<p><p>\u4e0a\u4e0b\u6587\u662f\u4f7f\u7528\u7b56\u7565\u7684\u89d2\u8272\uff0c\u5b83\u63a5\u53d7\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\uff0c\u5e76\u5c06\u8bf7\u6c42\u59d4\u6258\u7ed9\u67d0\u4e2a\u7b56\u7565\u3002\u5728Go\u8bed\u8a00\u4e2d\uff0c\u4e0a\u4e0b\u6587\u901a\u5e38\u662f\u4e00\u4e2a\u6301\u6709\u7b56\u7565\u63a5\u53e3\u7684\u7ed3\u6784\u4f53\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-go\">type Context struct {<\/p>\n<p>    strategy Strategy<\/p>\n<p>}<\/p>\n<p>func (c *Context) SetStrategy(strategy Strategy) {<\/p>\n<p>    c.strategy = strategy<\/p>\n<p>}<\/p>\n<p>func (c *Context) ExecuteStrategy() error {<\/p>\n<p>    \/\/ \u4f7f\u7528\u7b56\u7565\u63a5\u53e3\u7684\u65b9\u6cd5<\/p>\n<p>    return c.strategy.Execute()<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u7b56\u7565\u6a21\u5f0f\u7684\u5e94\u7528<\/h2>\n<\/p>\n<p><p>\u7b56\u7565\u6a21\u5f0f\u7684\u5e94\u7528\u5305\u62ec\u5ba2\u6237\u7aef\u9009\u62e9\u7b56\u7565\u5e76\u5c06\u5176\u8bbe\u7f6e\u5230\u4e0a\u4e0b\u6587\u4e2d\uff0c\u7136\u540e\u4e0a\u4e0b\u6587\u4f7f\u7528\u8be5\u7b56\u7565\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-go\">func m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n() {<\/p>\n<p>    context := &amp;Context{}<\/p>\n<p>    \/\/ \u5ba2\u6237\u7aef\u8bbe\u5b9a\u4f7f\u7528\u7b56\u7565A<\/p>\n<p>    context.SetStrategy(&amp;StrategyA{})<\/p>\n<p>    context.ExecuteStrategy()<\/p>\n<p>    \/\/ \u66f4\u6362\u7b56\u7565\u4e3aB<\/p>\n<p>    context.SetStrategy(&amp;StrategyB{})<\/p>\n<p>    context.ExecuteStrategy()<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7b56\u7565\u6a21\u5f0f\u4f7f\u5f97\u7b97\u6cd5\u53ef\u4ee5\u72ec\u7acb\u4e8e\u4f7f\u7528\u5b83\u4eec\u7684\u5ba2\u6237\u7aef\u53d8\u5316\uff0c\u589e\u52a0\u6216\u66ff\u6362\u72b6\u6001\u81ea\u7531\u5ea6\u9ad8\uff0c\u6613\u4e8e\u6269\u5c55\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u7b56\u7565\u6a21\u5f0f\u7684\u4f18\u7f3a\u70b9<\/h2>\n<\/p>\n<p><p>\u7b56\u7565\u6a21\u5f0f\u5e26\u6765\u4e86\u5f88\u591a\u597d\u5904\uff0c\u6bd4\u5982<strong>\u7b56\u7565\u53ef\u4ee5\u81ea\u7531\u5207\u6362\u3001\u907f\u514d\u4f7f\u7528\u591a\u91cd\u6761\u4ef6\u5224\u65ad\u3001\u6269\u5c55\u6027\u826f\u597d<\/strong>\u3002\u540c\u65f6\uff0c\u6bcf\u79cd\u7b56\u7565\u5b9e\u73b0\u81ea\u7531\u3001\u7b56\u7565\u4e4b\u95f4\u76f8\u4e92\u72ec\u7acb\uff0c\u7b26\u5408\u5f00\u95ed\u539f\u5219\u3002\u4f46\u662f\u7b56\u7565\u6a21\u5f0f\u4e5f\u6709\u7f3a\u70b9\uff0c\u5982\u679c\u7b56\u7565\u8fc7\u591a\u5219\u4f1a\u4ea7\u751f\u5f88\u591a\u7b56\u7565\u7c7b\uff0c\u7ba1\u7406\u548c\u7406\u89e3\u6210\u672c\u4f1a\u589e\u52a0\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u5b9a\u4e49\u660e\u786e\u7684\u7b56\u7565\u63a5\u53e3\uff0c\u91c7\u7528Go\u8bed\u8a00\u7684\u63a5\u53e3\u7279\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u677e\u8026\u5408\u7684\u8bbe\u8ba1\uff0c\u5728\u8fd0\u884c\u65f6\u7075\u6d3b\u5730\u66ff\u6362\u4e0d\u540c\u7684\u7b97\u6cd5\uff0c\u4ece\u800c\u4f7f\u5f97\u4ee3\u7801\u6613\u4e8e\u6269\u5c55\u548c\u7ef4\u62a4\u3002\u8fd9\u79cd\u6a21\u5f0f\u5728\u8bb8\u591a\u5e94\u7528\u573a\u666f\uff0c\u5982\u5404\u79cd\u6392\u5e8f\u3001\u8f6c\u6362\u3001\u9009\u62e9\u64cd\u4f5c\u7b49\uff0c\u90fd\u8868\u73b0\u51fa\u4e86\u5de8\u5927\u7684\u7075\u6d3b\u6027\u548c\u5b9e\u7528\u4ef7\u503c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5728Go\u8bed\u8a00\u4e2d\uff0c\u5982\u4f55\u5b9e\u73b0\u7b56\u7565\u6a21\u5f0f\uff1f<\/strong><br \/>\n\u7b56\u7565\u6a21\u5f0f\u662f\u4e00\u79cd\u8f6f\u4ef6\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u5b83\u5141\u8bb8\u5728\u8fd0\u884c\u65f6\u6839\u636e\u4e0d\u540c\u7684\u60c5\u51b5\u9009\u62e9\u4e0d\u540c\u7684\u7b97\u6cd5\u6216\u884c\u4e3a\u3002\u5728Go\u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u63a5\u53e3\u548c\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u6765\u5b9e\u73b0\u7b56\u7565\u6a21\u5f0f\u3002\u5177\u4f53\u5b9e\u73b0\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a\u63a5\u53e3\uff0c\u5b9a\u4e49\u4e0d\u540c\u7b56\u7565\u6240\u9700\u7684\u65b9\u6cd5\u3002<\/li>\n<li>\u521b\u5efa\u4e0d\u540c\u7684\u7ed3\u6784\u4f53\u5e76\u5b9e\u73b0\u63a5\u53e3\u4e2d\u7684\u65b9\u6cd5\uff0c\u6bcf\u4e2a\u7ed3\u6784\u4f53\u4ee3\u8868\u4e00\u79cd\u5177\u4f53\u7b56\u7565\u3002<\/li>\n<li>\u5728\u9700\u8981\u4f7f\u7528\u7b56\u7565\u7684\u5730\u65b9\uff0c\u8c03\u7528\u63a5\u53e3\u65b9\u6cd5\uff0c\u4f20\u5165\u4e0d\u540c\u7684\u7b56\u7565\u7ed3\u6784\u4f53\u5b9e\u4f8b\u5373\u53ef\u5b9e\u73b0\u4e0d\u540c\u7b56\u7565\u7684\u9009\u62e9\u3002<\/li>\n<\/ol>\n<p><strong>2. \u5728Go\u8bed\u8a00\u4e2d\u4f7f\u7528\u7b56\u7565\u6a21\u5f0f\u6709\u4ec0\u4e48\u4f18\u52bf\uff1f<\/strong><br \/>\n\u4f7f\u7528\u7b56\u7565\u6a21\u5f0f\u7684\u4f18\u52bf\u4e4b\u4e00\u662f\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u8fd0\u884c\u65f6\u5207\u6362\u4e0d\u540c\u7684\u7b97\u6cd5\u6216\u884c\u4e3a\u3002\u901a\u8fc7\u5c06\u7b56\u7565\u5c01\u88c5\u5728\u72ec\u7acb\u7684\u7ed3\u6784\u4f53\u4e2d\uff0c\u53ef\u4ee5\u5b9e\u73b0\u7b56\u7565\u7684\u89e3\u8026\uff0c\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u7075\u6d3b\u548c\u53ef\u6269\u5c55\u3002\u6b64\u5916\uff0c\u7b56\u7565\u6a21\u5f0f\u8fd8\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u53ef\u8bfb\u6027\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u5177\u4f53\u7684\u7b56\u7565\u90fd\u88ab\u5c01\u88c5\u5728\u4e00\u4e2a\u72ec\u7acb\u7684\u7ed3\u6784\u4f53\u4e2d\uff0c\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\u3002<\/p>\n<p><strong>3. Go\u8bed\u8a00\u4e2d\u7b56\u7565\u6a21\u5f0f\u7684\u9002\u7528\u573a\u666f\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\n\u7b56\u7565\u6a21\u5f0f\u9002\u7528\u4e8e\u4ee5\u4e0b\u60c5\u51b5\uff1a<\/p>\n<ul>\n<li>\u5f53\u9700\u8981\u6839\u636e\u4e0d\u540c\u6761\u4ef6\u6216\u53c2\u6570\u9009\u62e9\u4e0d\u540c\u7b97\u6cd5\u6216\u884c\u4e3a\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u7b56\u7565\u6a21\u5f0f\u3002\u6bd4\u5982\uff0c\u5728\u4e00\u4e2a\u7535\u5546\u5e73\u53f0\u4e2d\uff0c\u6839\u636e\u4e0d\u540c\u7528\u6237\u7684\u8d2d\u4e70\u4e60\u60ef\uff0c\u9009\u62e9\u4e0d\u540c\u7684\u4f18\u60e0\u7b56\u7565\u3002<\/li>\n<li>\u5f53\u7b97\u6cd5\u6216\u884c\u4e3a\u7684\u590d\u6742\u6027\u8f83\u5927\uff0c\u5e76\u4e14\u53ef\u80fd\u4f1a\u7ecf\u5e38\u53d8\u66f4\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u7b56\u7565\u6a21\u5f0f\u3002\u901a\u8fc7\u5c06\u4e0d\u540c\u7b97\u6cd5\u6216\u884c\u4e3a\u5c01\u88c5\u5728\u72ec\u7acb\u7684\u7ed3\u6784\u4f53\u4e2d\uff0c\u53ef\u4ee5\u66f4\u52a0\u8f7b\u677e\u5730\u8fdb\u884c\u7ef4\u62a4\u548c\u6269\u5c55\u3002<\/li>\n<li>\u5f53\u9700\u8981\u5728\u4e0d\u540c\u5bf9\u8c61\u6216\u4e1a\u52a1\u573a\u666f\u4e2d\u590d\u7528\u76f8\u540c\u7684\u7b97\u6cd5\u6216\u884c\u4e3a\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u7b56\u7565\u6a21\u5f0f\u3002\u901a\u8fc7\u5c01\u88c5\u4e0d\u540c\u7b56\u7565\u7684\u7ed3\u6784\u4f53\uff0c\u5e76\u5728\u9700\u8981\u7684\u5730\u65b9\u8fdb\u884c\u8c03\u7528\uff0c\u53ef\u4ee5\u5b9e\u73b0\u590d\u7528\u6027\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"\u7b56\u7565\u6a21\u5f0f\u662f\u4e00\u79cd\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u5b83\u5141\u8bb8\u5728\u8fd0\u884c\u65f6\u9009\u62e9\u6700\u5408\u9002\u7684\u7b97\u6cd5\u6216\u884c\u4e3a\u3002\u5b83\u5b9a\u4e49\u4e86\u4e00\u7cfb\u5217\u7684\u7b97\u6cd5\uff0c\u5e76\u5c06\u6bcf\u4e2a\u7b97\u6cd5\u5c01\u88c5\u8d77\u6765\uff0c\u4f7f\u5b83 [&hellip;]","protected":false},"author":3,"featured_media":309149,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/309130"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=309130"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/309130\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/309149"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=309130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=309130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=309130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}