{"id":207286,"date":"2025-07-08T10:21:42","date_gmt":"2025-07-08T02:21:42","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207286\/"},"modified":"2025-07-08T10:21:42","modified_gmt":"2025-07-08T02:21:42","slug":"%e8%8e%b7%e5%8f%96%e7%bb%93%e6%9e%84%e5%90%8d%e7%a7%b0%e8%80%8c%e4%b8%8d%e8%8e%b7%e5%8f%96%e5%8c%85%e5%90%8d%e7%a7%b0%e6%88%96%e6%8c%87%e9%92%88","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207286\/","title":{"rendered":"\u83b7\u53d6\u7ed3\u6784\u540d\u79f0\u800c\u4e0d\u83b7\u53d6\u5305\u540d\u79f0\u6216\u6307\u9488"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u83b7\u53d6\u7ed3\u6784\u540d\u79f0\u800c\u4e0d\u83b7\u53d6\u5305\u540d\u79f0\u6216\u6307\u9488<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-21 14:12:35<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u6765\u5230\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60Golang\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300a\u83b7\u53d6\u7ed3\u6784\u540d\u79f0\u800c\u4e0d\u83b7\u53d6\u5305\u540d\u79f0\u6216\u6307\u9488\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6709\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u4ee3\u7801\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>package chain_of_responsibility\n\nimport (\n    \"fmt\"\n    \"reflect\"\n)\n\ntype customerbalancerequest struct{\n    customername string\n    balance int\n}\n\ntype balancerequest interface {\n    handle(request customerbalancerequest)\n}\n\ntype headeditor struct{\n    next balancerequest\n}\n\nfunc (h *headeditor) handle(b customerbalancerequest){\n    if b.balance &lt; 1000 {\n        fmt.printf(\"%t approved balance for %v request. balance: %v\\n\", h, b.customername, b.balance)\n        fmt.printf(\"%v approved balance for %v request. balance: %v\\n\", reflect.typeof(h), b.customername, b.balance)\n        fmt.printf(\"%v approved balance for %v request. balance: %v\\n\", reflect.typeof(h).string(), b.customername, b.balance)\n        fmt.printf(\"%v approved balance for %v request. balance: %v\\n\", reflect.typeof(h).name(), b.customername, b.balance)\n\n    } else{\n        h.next.handle(b)\n    }\n}<\/pre>\n<p>\u5728 fmt.printf \u884c\u4e0a\uff0c\u6211\u60f3\u6253\u5370 headeditor \u7c7b\u578b\u7684\u540d\u79f0\u3002\u6211\u4f7f\u7528\u5404\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u8fd9\u5c31\u662f\u6211\u7684\u7ed3\u679c\uff1a<\/p>\n<pre>*chain_of_responsibility.HeadEditor approved balance for John request. Balance: 500\n*chain_of_responsibility.HeadEditor approved balance for John request. Balance: 500\n*chain_of_responsibility.HeadEditor approved balance for John request. Balance: 500\n approved balance for John request. Balance: 500<\/pre>\n<p>\u95ee\u9898\u662f\u5728\u524d 3 \u4e2a printf \u8c03\u7528\u4e2d\uff0c\u6211\u53ef\u4ee5\u83b7\u5f97\u7c7b\u578b\u7684\u540d\u79f0\uff0c\u4f46\u5b83\u4eec\u5305\u62ec\u6307\u9488\u548c\u5305\u540d\u79f0\u3002\u6709\u4ec0\u4e48\u65b9\u6cd5\u53ef\u4ee5\u8ba9\u6211\u53ea\u83b7\u5f97\u6ca1\u6709\u5305\u540d\u79f0\u548c\u6307\u9488\u7684\u201cheadeditor\u201d\uff0c\u5f53\u7136\u9664\u4e86\u5b57\u7b26\u4e32\u5904\u7406\u89e3\u51b3\u65b9\u6848\uff08\u4f8b\u5982\u4ece\u7ed3\u679c\u4e2d\u5220\u9664 * \u548c\u5305\u540d\u79f0\uff09\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u5df2\u7ecf\u63a5\u8fd1\u6700\u540e\u4e00\u4e2a\u4e86\u3002\u6b63\u5982 <code>name()<\/code> \u7684\u6587\u6863\u6240\u8ff0\uff1a<\/p>\n<pre>\/\/ name returns the type's name within its package for a defined type.\n\/\/ for other (non-defined) types it returns the empty string.<\/pre>\n<p>\u60a8\u5c06\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\uff0c\u56e0\u4e3a\u867d\u7136 <code>chain_of_responsibility.headeditor<\/code> \u662f\u5b9a\u4e49\u7684\u7c7b\u578b\uff0c\u4f46 <code>*chain_of_responsibility.headeditor<\/code> \u4e0d\u662f\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>elem()<\/code> \u4ece\u6307\u9488\u7c7b\u578b\u4e2d\u83b7\u53d6\u7c7b\u578b\uff1a<\/p>\n<pre>\/\/ Elem returns a type's element type.\n\/\/ It panics if the type's Kind is not Array, Chan, Map, Ptr, or Slice.<\/pre>\n<p>\u56e0\u6b64\uff0c\u5982\u679c\u5b83\u5e76\u4e0d\u603b\u662f\u4e00\u4e2a\u6307\u9488\uff0c\u90a3\u4e48\u5728\u8c03\u7528 <code>elem()<\/code> \u4e4b\u524d\uff0c\u60a8\u9700\u8981\u9996\u5148\u68c0\u67e5\u5b83\u662f\u5426\u662f\u4e00\u4e2a\u6307\u9488\u3002<\/p>\n<p>\u6216\u8005\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u53cd\u5c04\u6765\u4f7f\u4ee3\u7801\u53d8\u5f97\u66f4\u7b80\u5355\uff08\u5e76\u4e14\u53ef\u80fd\u66f4\u5feb\uff09\uff0c\u5e76\u4e3a\u60a8\u7684\u7c7b\u578b\u63d0\u4f9b\u4e00\u4e2a\u8fd4\u56de\u60a8\u60f3\u8981\u7528\u4e8e\u6bcf\u79cd\u7c7b\u578b\u7684\u4efb\u4f55\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\uff0c\u4f8b\u5982 <code>type() string<\/code>\u3002\u7136\u540e\uff0c\u60a8\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a <code>typer<\/code> \u63a5\u53e3\u6765\u5c01\u88c5\u8be5\u65b9\u6cd5\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u83b7\u53d6\u7ed3\u6784\u540d\u79f0\u800c\u4e0d\u83b7\u53d6\u5305\u540d\u79f0\u6216\u6307\u9488\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207286","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207286","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207286"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207286\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}