{"id":41856,"date":"2024-12-01T10:42:01","date_gmt":"2024-12-01T02:42:01","guid":{"rendered":"https:\/\/fwq.ai\/blog\/41856\/"},"modified":"2024-12-01T10:42:01","modified_gmt":"2024-12-01T02:42:01","slug":"%e5%90%8c%e4%b8%80%e6%96%b9%e6%b3%95%e7%9a%84%e5%a4%9a%e4%b8%aa%e5%ae%9e%e7%8e%b0%e5%8f%af%e8%83%bd%e4%b8%8e-go-%e5%92%8c%e6%8e%a5%e5%8f%a3%e6%9c%89%e4%b8%8d%e5%90%8c%e7%9a%84%e4%be%9d%e8%b5%96","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/41856\/","title":{"rendered":"\u540c\u4e00\u65b9\u6cd5\u7684\u591a\u4e2a\u5b9e\u73b0\u53ef\u80fd\u4e0e Go \u548c\u63a5\u53e3\u6709\u4e0d\u540c\u7684\u4f9d\u8d56\u5173\u7cfb"},"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>\u540c\u4e00\u65b9\u6cd5\u7684\u591a\u4e2a\u5b9e\u73b0\u53ef\u80fd\u4e0e Go \u548c\u63a5\u53e3\u6709\u4e0d\u540c\u7684\u4f9d\u8d56\u5173\u7cfb<\/span><\/p>\n<h1>\u540c\u4e00\u65b9\u6cd5\u7684\u591a\u4e2a\u5b9e\u73b0\u53ef\u80fd\u4e0e Go \u548c\u63a5\u53e3\u6709\u4e0d\u540c\u7684\u4f9d\u8d56\u5173\u7cfb<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-21 18:48:38<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u540c\u4e00\u65b9\u6cd5\u7684\u591a\u4e2a\u5b9e\u73b0\u53ef\u80fd\u4e0e Go \u548c\u63a5\u53e3\u6709\u4e0d\u540c\u7684\u4f9d\u8d56\u5173\u7cfb\u300b<\/span>\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span><\/span>\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u5728\u4f7f\u7528\u754c\u9762\u65f6\u9047\u5230\u95ee\u9898\u3002<\/p>\n<p>\u6211\u6709\u4e00\u4e2a compute(a, b int) \u65b9\u6cd5\uff0c\u5b83\u6709 2 \u4e2a\u5b9e\u73b0\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u63a5\u6536\u5668\u3002<\/p>\n<pre>func (addition *addition) compute(a, b int) int{\n    return a+b\n}\n\nfunc (mult *multiplication) compute(a, b int) int{\n    return a*b\n}\n\n\ntype myinterface{\n    compute(a, b int) int\n}<\/pre>\n<p>\u5047\u8bbe\u6211\u9700\u8981\u5728\u4e58\u6cd5\u4e2d\u8c03\u7528 webservice \u6765\u83b7\u53d6 a \u7684\u503c\u3002<\/p>\n<p>\u73b0\u5728\u6211\u4eec\u6709\uff1a<\/p>\n<pre>func (mult *multiplication) compute(iface wsinterface, a, b int) int{\n    a := iface.geta()\n    return a*b\n}<\/pre>\n<p>\u73b0\u5728\uff0c\u6211\u9700\u8981\u5c06 <code>iface wsinterface<\/code> \u6dfb\u52a0\u5230 <code>compute()<\/code> \u63a5\u53e3\u5b9a\u4e49\u4e2d\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230 addition \u4e2d\uff0c\u5373\u4f7f\u5b83\u4e0d\u9700\u8981\u5b83\u3002<\/p>\n<p>\u6211\u5c06\u4ee5\uff1a<\/p>\n<p>\u7ed3\u675f <\/p>\n<pre>type Addition struct{}\n\ntype Multiplication struct{}\n\n\nfunc main() {\n    addition := &amp;Addition{}\n    multiplication := &amp;Multiplication{}\n    res1 := addition.Compute(nil, 1, 2)\n    res2 := addition.Compute(multiplication, 3, 4)\n    fmt.Print(res1, res2)\n}\n\ntype WSInterface interface {\n    getA() int\n}\n\nfunc (mult *Multiplication) getA() int {\n    return 1 \/\/ Mocked\n}\n\ntype myInterface interface {\n    Compute(iface myInterface, a, b int) int\n}\n\nfunc (addition *Addition) Compute(iface WSInterface, a, b int) int {\n    return a + b\n}\n\nfunc (mult *Multiplication) Compute(iface WSInterface, a, b int) int {\n    return iface.getA() * b\n}<\/pre>\n<p>\u4f46\u9664\u6b64\u4e4b\u5916\uff0c\u5b83\u4e0d\u4f1a\u88ab\u4f7f\u7528\u3002<\/p>\n<p>\u5728\u73b0\u5b9e\u751f\u6d3b\u4e2d\uff0c\u60a8\u53ef\u4ee5\u5bf9\u4e0d\u540c\u7684\u5fae\u670d\u52a1\u6709\u591a\u4e2a\u4f9d\u8d56\u9879\uff0c\u5e76\u4e14\u6211\u53d1\u73b0\u5b9a\u4e49\u4e0d\u4f1a\u5728\u51fd\u6570\u4e2d\u4f7f\u7528\u7684\u53c2\u6570\u5e76\u4e0d\u662f\u90a3\u4e48\u4f18\u96c5\u3002\u8fd9\u91cc\u80af\u5b9a\u6709\u4ec0\u4e48\u95ee\u9898\u3002<\/p>\n<p>\u8fd9\u6837\u505a\u53ef\u4ee5\u5417\uff1f\u8fd9\u662f\u4e00\u4e2a\u4e0d\u597d\u7684\u6a21\u5f0f\u5417\uff1f\u6216\u8005\u6211\u5e94\u8be5\u5982\u4f55\u4fee\u590d\u5b83\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u8fd9\u4e9b\u7279\u6b8a\u7684\u63a5\u53e3\/\u5bf9\u8c61\u5e94\u8be5\u5728\u6784\u9020\u51fd\u6570\u4e2d\u4f20\u9012\uff0c\u4fdd\u6301\u63a5\u53e3\u672c\u8eab\u7684\u5e72\u51c0\u3002<\/p>\n<p>\u7c7b\u4f3c\u4e8e\uff1a<\/p>\n<pre>type multiplication struct {\n  iface otherinferface\n  \/\/ .. other multiplication-specific fields\n}\n\nfunc newmultiplication(iface otherinterface) *multiplication {\n  return &amp;multiplication{iface: iface}\n}<\/pre>\n<p>\u7136\u540e\uff1a<\/p>\n<pre>func (mult *multiplication) compute(a, b int) int{\n    a := mult.iface.geta()\n    return a*b\n}<\/pre>\n<p>\u56e0\u6b64\u60a8\u7684 <code>myinterface<\/code> \u4ecd\u7136\u7b80\u5355\u5e72\u51c0\uff1a<\/p>\n<pre>type myInterface interface {\n  Compute(a, b int)\n}<\/pre>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u540c\u4e00\u65b9\u6cd5\u7684\u591a\u4e2a\u5b9e\u73b0\u53ef\u80fd\u4e0e Go \u548c\u63a5\u53e3\u6709\u4e0d\u540c\u7684\u4f9d\u8d56\u5173\u7cfb\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\u7c73\u4e91\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; &gt; &gt; \u540c\u4e00\u65b9\u6cd5\u7684\u591a\u4e2a\u5b9e\u73b0\u53ef\u80fd\u4e0e Go \u548c\u63a5\u53e3\u6709\u4e0d\u540c\u7684\u4f9d\u8d56\u5173\u7cfb \u540c\u4e00\u65b9\u6cd5\u7684\u591a\u4e2a\u5b9e\u73b0\u53ef\u80fd\u4e0e Go \u548c\u63a5\u53e3\u6709\u4e0d\u540c\u7684\u4f9d\u8d56\u5173\u7cfb \u6765\u6e90\uff1astackoverflow 2024-04-21 18:48:38 0\u6d4f\u89c8 \u6536\u85cf \u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0\u300a\u540c\u4e00\u65b9\u6cd5\u7684\u591a\u4e2a\u5b9e\u73b0\u53ef\u80fd\u4e0e Go \u548c\u63a5\u53e3\u6709\u4e0d\u540c\u7684\u4f9d\u8d56\u5173\u7cfb\u300b\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230\uff0c\u5982\u679c\u4f60\u5bf9Golang\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01 \u95ee\u9898\u5185\u5bb9 \u6211\u5728\u4f7f\u7528\u754c\u9762\u65f6\u9047\u5230\u95ee\u9898\u3002 \u6211\u6709\u4e00\u4e2a compute(a, b int) \u65b9\u6cd5\uff0c\u5b83\u6709 2 \u4e2a\u5b9e\u73b0\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u63a5\u6536\u5668\u3002 func (addition *addition) compute(a, b int) int{ return a+b } func (mult *multiplication) compute(a, b int) int{ return a*b } type myinterface{ compute(a, b int) int } [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-41856","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/41856","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=41856"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/41856\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=41856"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=41856"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=41856"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}