{"id":208331,"date":"2025-07-08T13:56:46","date_gmt":"2025-07-08T05:56:46","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208331\/"},"modified":"2025-07-08T13:56:46","modified_gmt":"2025-07-08T05:56:46","slug":"%e5%9c%a8-go-%e4%b8%ad%e5%90%91%e6%9e%84%e9%80%a0%e5%87%bd%e6%95%b0%e6%b7%bb%e5%8a%a0%e5%8f%af%e5%8f%98%e5%8f%82%e6%95%b0%e6%97%b6%e5%a6%82%e4%bd%95%e8%bf%9b%e8%a1%8c%e7%89%88%e6%9c%ac%e6%8e%a7","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208331\/","title":{"rendered":"\u5728 Go \u4e2d\u5411\u6784\u9020\u51fd\u6570\u6dfb\u52a0\u53ef\u53d8\u53c2\u6570\u65f6\u5982\u4f55\u8fdb\u884c\u7248\u672c\u63a7\u5236\uff1f"},"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>\u5728 Go \u4e2d\u5411\u6784\u9020\u51fd\u6570\u6dfb\u52a0\u53ef\u53d8\u53c2\u6570\u65f6\u5982\u4f55\u8fdb\u884c\u7248\u672c\u63a7\u5236\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-05-01 21:00:34<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300a\u5728 Go \u4e2d\u5411\u6784\u9020\u51fd\u6570\u6dfb\u52a0\u53ef\u53d8\u53c2\u6570\u65f6\u5982\u4f55\u8fdb\u884c\u7248\u672c\u63a7\u5236\uff1f\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u76f8\u4fe1\u5411\u5e38\u89c4\u51fd\u6570\u6216\u65b9\u6cd5\u6dfb\u52a0\u53ef\u53d8\u53c2\u6570\u51fd\u6570\u6b63\u5728\u7834\u574f\u57fa\u4e8e\u8fd9\u7bc7\u6587\u7ae0\u7684\u66f4\u6539\u3002\u4f46\u662f\u5411\u6784\u9020\u51fd\u6570\u6dfb\u52a0\u53ef\u53d8\u53c2\u6570\u600e\u4e48\u6837\uff1f\u4f8b\u5982\u6dfb\u52a0\u529f\u80fd\u9009\u9879\u3002<\/p>\n<p>\u901a\u8fc7\u4f7f\u7528 semver\uff0c\u6b64\u4ee3\u7801\u4e3a v1.0.0<\/p>\n<pre>type foo struct{}\n\n\/\/ constructor with default behavior\nfunc newfoo() *foo {\n    return &amp;foo{}\n}<\/pre>\n<p>\u6dfb\u52a0\u53ef\u53d8\u53c2\u6570<\/p>\n<pre>type Foo struct{}\n\ntype Option func(&amp;Foo)\n\n\/\/ constructor with option\nfunc NewFoo(opts ...Option) *Foo {\n    \/\/ process option before return\n    \/\/ ...\n    return &amp;Foo{}\n}<\/pre>\n<p>\u5728\u8c03\u7528\u6784\u9020\u51fd\u6570\u65f6\uff0c\u65e7\u4ee3\u7801\u4ecd\u7136\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\uff0c\u5e76\u4e14\u6ca1\u6709\u4eba\u5c06\u6784\u9020\u51fd\u6570\u5206\u914d\u7ed9\u53d8\u91cf\u5e76\u5c06\u6784\u9020\u51fd\u6570\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\uff0c\u5c31\u50cf\u672c\u4f8b\u4e00\u6837\u3002<\/p>\n<p>\u90a3\u4e48\uff0c\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u5e94\u8be5\u5c06\u4e3b\u8981\u7248\u672c\u589e\u52a0\u5230 v2.0.0 \u8fd8\u662f\u5c06\u6b21\u8981\u7248\u672c\u589e\u52a0\u5230 v1.1.0\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u5728 go \u4e2d\uff0c\u6211\u5e94\u8be5\u5c06\u4e3b\u8981\u7248\u672c\u589e\u52a0\u5230 v2.0.0 \u8fd8\u662f\u6b21\u8981\u7248\u672c \u5230 v1.1.0\uff1f<\/p>\n<p>\u5728 go \u4e2d\uff0c<code>newfoo<\/code> \u51fd\u6570\u7c7b\u578b <\/p>\n<p><code>v1.0.0<\/code>\uff1a<\/p>\n<pre>type foo struct{}\n\nfunc newfoo() *foo {}<\/pre>\n<p>\u548c<\/p>\n<p><code>v2.0.0<\/code>\uff1a<\/p>\n<pre>type foo struct{}\n\ntype option func(*foo)\n\nfunc newfoo(opts ...option) *foo { }<\/pre>\n<p>\u662f\u4e0d\u540c\u7684\u7c7b\u578b\u3002\u8fd9\u4f7f\u5176\u6210\u4e3a\u4e00\u9879\u91cd\u5927\u66f4\u6539\uff0c\u5373\u5bf9\u4e3b\u8981\u7248\u672c\u53f7\u7684\u66f4\u65b0\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u8fd9\u5e76\u4e0d\u662f\u771f\u6b63\u7684\u91cd\u5927\u66f4\u6539\uff0c\u53ea\u662f\u6269\u5c55\uff0c\u56e0\u6b64\u8bf7\u66f4\u65b0\u6b21\u8981\u7248\u672c\u53f7\u3002\u4f7f\u7528\u76f8\u4f3c\u4f46\u4e0d\u540c\u7684\u51fd\u6570\u540d\u79f0\u3002\u4f8b\u5982\uff0c<code>newfoo<\/code> \u548c <code>newfooopts<\/code>\u3002<\/p>\n<p><code>v1.1.0<\/code>\uff1a<\/p>\n<pre>type Foo struct{}\n\nfunc NewFoo() *Foo {\n    return NewFooOpts()\n}\n\ntype Option func(*Foo)\n\nfunc NewFooOpts(opts ...Option) *Foo {\n    var foo Foo\n    for _, opt := range opts {\n        \/\/ handle opt\n        _ = opt\n    }\n    return &amp;foo\n}<\/pre>\n<p>\u6f14\u793a\uff1a<\/p>\n<p>\u4f7f\u7528\u8bb0\u5f55\u51fd\u6570\u7684\u6ce8\u91ca\u4e3a\u7528\u6237\u63d0\u4f9b\u6709\u5173\u4f7f\u7528\u54ea\u4e2a\u51fd\u6570\u7684\u6307\u5bfc\u3002\u4f8b\u5982\uff0c\u4e5f\u8bb8\u51fd\u6570 <code>newfoo<\/code> \u5df2\u88ab\u5f03\u7528\uff0c\u53d6\u800c\u4ee3\u4e4b\u7684\u662f\u51fd\u6570 <code>newfooopts<\/code>\u3002<\/p>\n<p>\u6ca1\u6709\u4eba\u5c06\u6784\u9020\u51fd\u6570\u5206\u914d\u7ed9\u53d8\u91cf\u5e76\u4f20\u9012 \u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u6784\u9020\u51fd\u6570\u3002<\/p>\n<p>\u65e2\u7136\u6709\u4eba\u8fd9\u6837\u505a\u4e86\uff0c\u90a3\u4e48\u8fd9\u4e2a\u8bf4\u6cd5\u5c31\u662f\u9519\u8bef\u7684\u3002<\/p>\n<p>\u6211\u8ba4\u4e3a\u4e24\u8005\u90fd\u53ef\u4ee5\uff0c\u56e0\u4e3a\u8fd9\u662f\u4e00\u4e2a\u7070\u8272\u5730\u5e26\u3002\u539f\u5219\u4e0a\uff0c\u60a8\u4f1a\u7834\u574f\u5411\u540e\u517c\u5bb9\u6027\uff0c\u56e0\u4e3a\u51fd\u6570\u7684\u7b7e\u540d\u53d1\u751f\u4e86\u53d8\u5316\uff0c\u4f46\u5b9e\u9645\u4e0a\u8c03\u7528\u8005\u4e0d\u592a\u53ef\u80fd\u53d7\u5230\u5f71\u54cd\u3002<\/p>\n<p>\u8fd9\u76f8\u5f53\u4e8e\u5c06\u5b57\u6bb5\u6dfb\u52a0\u5230\u7ed3\u6784\u4e2d\uff0c\u8be5\u7ed3\u6784\u5bf9\u65b0\u5b57\u6bb5\u5177\u6709\u5408\u7406\u7684\u9ed8\u8ba4\u96f6\u503c\u89e3\u91ca\u3002\u4f7f\u7528\u8fd9\u4e9b\u7ed3\u6784\u7684\u4ee3\u7801\u53ef\u80fd\u4f1a\u6709\u4e0d\u540c\u7684\u884c\u4e3a\uff08\u4f8b\u5982\uff0c\u5982\u679c\u4ed6\u4eec\u67e5\u770b\u7ed3\u6784\u7684\u5927\u5c0f\uff0c\u6216\u8005\u5982\u679c\u4ed6\u4eec\u4f7f\u7528\u53cd\u5c04\uff09\uff0c\u4f46\u79f0\u5176\u4e3a\u7834\u574f\u6027\u66f4\u6539\u662f\u4e0d\u52a1\u5b9e\u7684\uff0c\u9664\u975e\u60a8\u671f\u671b\u4eba\u4eec\u6b63\u5728\u6267\u884c\u8fd9\u4e9b\u8fb9\u7f18\u60c5\u51b5\u4e8b\u60c5\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u5728 Go \u4e2d\u5411\u6784\u9020\u51fd\u6570\u6dfb\u52a0\u53ef\u53d8\u53c2\u6570\u65f6\u5982\u4f55\u8fdb\u884c\u7248\u672c\u63a7\u5236\uff1f\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-208331","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208331","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=208331"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208331\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208331"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}