{"id":861,"date":"2024-11-07T12:22:27","date_gmt":"2024-11-07T04:22:27","guid":{"rendered":"https:\/\/fwq.ai\/blog\/861\/"},"modified":"2024-11-07T12:22:27","modified_gmt":"2024-11-07T04:22:27","slug":"go-%e8%af%ad%e8%a8%80-interface-%e8%83%bd%e5%90%a6%e5%a3%b0%e6%98%8e%e5%b1%9e%e6%80%a7%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/861\/","title":{"rendered":"Go \u8bed\u8a00 Interface \u80fd\u5426\u58f0\u660e\u5c5e\u6027\uff1f"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1052\" src=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173017800466250.jpg\" width=\"800\" height=\"320\" srcset=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173017800466250.jpg 800w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173017800466250-300x120.jpg 300w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173017800466250-768x307.jpg 768w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173017800466250-670x268.jpg 670w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" title=\"Go \u8bed\u8a00 Interface \u80fd\u5426\u58f0\u660e\u5c5e\u6027\uff1f\u63d2\u56fe\" alt=\"Go \u8bed\u8a00 Interface \u80fd\u5426\u58f0\u660e\u5c5e\u6027\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>go \u8bed\u8a00\u4e2d interface \u80fd\u5426\u58f0\u660e\u5c5e\u6027\uff1f<\/strong><\/p>\n<p>\u5728 go \u8bed\u8a00\u4e2d\uff0cinterface \u4e3b\u8981\u7528\u4e8e\u5b9a\u4e49\u4e00\u7ec4\u65b9\u6cd5\uff08\u884c\u4e3a\uff09\u7684\u96c6\u5408\uff0c\u5b83\u672c\u8eab\u5e76\u4e0d\u5305\u542b\u5b9e\u4f53\u5c5e\u6027\u3002\u56e0\u6b64\uff0c\u5728 interface \u4e2d\u58f0\u660e\u5c5e\u6027 servicename \u662f\u4e0d\u5141\u8bb8\u7684\u3002<\/p>\n<p><strong>\u66ff\u4ee3\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u5982\u679c\u60a8\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u5177\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u7684\u5bf9\u8c61\uff0c\u53ef\u4ee5\u4f7f\u7528\u7ed3\u6784\u4f53\uff08struct\uff09\u3002\u7ed3\u6784\u4f53\u662f go \u8bed\u8a00\u4e2d\u5b9a\u4e49\u5b9e\u4f53\u7684\u5bb9\u5668\uff0c\u5b83\u53ef\u4ee5\u540c\u65f6\u5305\u542b\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>type Router struct {\n    ServiceName string\n    Route(msgB []byte) error\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u6837\uff0c\u60a8\u5c31\u53ef\u4ee5\u901a\u8fc7\u521d\u59cb\u5316\u7ed3\u6784\u4f53\u6765\u521b\u5efa\u4e00\u4e2a\u5177\u6709\u6307\u5b9a servicename \u5c5e\u6027\u7684 router \u5bf9\u8c61\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fGo \u8bed\u8a00 Interface \u80fd\u5426\u58f0\u660e\u5c5e\u6027\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>go \u8bed\u8a00\u4e2d interface \u80fd\u5426\u58f0\u660e\u5c5e\u6027\uff1f \u5728 go \u8bed\u8a00\u4e2d\uff0cinterface \u4e3b\u8981\u7528\u4e8e\u5b9a\u4e49\u4e00\u7ec4\u65b9\u6cd5\uff08\u884c\u4e3a\uff09\u7684\u96c6\u5408\uff0c\u5b83\u672c\u8eab\u5e76\u4e0d\u5305\u542b\u5b9e\u4f53\u5c5e\u6027\u3002\u56e0\u6b64\uff0c\u5728 interface \u4e2d\u58f0\u660e\u5c5e\u6027 servicename \u662f\u4e0d\u5141\u8bb8\u7684\u3002 \u66ff\u4ee3\u89e3\u51b3\u65b9\u6848 \u5982\u679c\u60a8\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u5177\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u7684\u5bf9\u8c61\uff0c\u53ef\u4ee5\u4f7f\u7528\u7ed3\u6784\u4f53\uff08struct\uff09\u3002\u7ed3\u6784\u4f53\u662f go \u8bed\u8a00\u4e2d\u5b9a\u4e49\u5b9e\u4f53\u7684\u5bb9\u5668\uff0c\u5b83\u53ef\u4ee5\u540c\u65f6\u5305\u542b\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a type Router struct { ServiceName string Route(msgB []byte) error } \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u6837\uff0c\u60a8\u5c31\u53ef\u4ee5\u901a\u8fc7\u521d\u59cb\u5316\u7ed3\u6784\u4f53\u6765\u521b\u5efa\u4e00\u4e2a\u5177\u6709\u6307\u5b9a servicename \u5c5e\u6027\u7684 router \u5bf9\u8c61\u3002 \u4ee5\u4e0a\u5c31\u662fGo \u8bed\u8a00 Interface \u80fd\u5426\u58f0\u660e\u5c5e\u6027\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-861","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/861","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=861"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/861\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=861"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=861"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=861"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}