{"id":205774,"date":"2025-06-12T10:20:07","date_gmt":"2025-06-12T02:20:07","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205774\/"},"modified":"2025-06-12T10:20:07","modified_gmt":"2025-06-12T02:20:07","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e9%97%ad%e5%8c%85%e5%9c%a8-golang-%e4%b8%ad%e5%88%9b%e5%bb%ba%e4%b8%80%e4%b8%aa%e5%8c%bf%e5%90%8d%e5%87%bd%e6%95%b0%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205774\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528\u95ed\u5305\u5728 Golang \u4e2d\u521b\u5efa\u4e00\u4e2a\u533f\u540d\u51fd\u6570\uff1f"},"content":{"rendered":"<p>\u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762GOLANG\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2aGOLANG\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300a\u5982\u4f55\u4f7f\u7528\u95ed\u5305\u5728 GOLANG \u4e2d\u521b\u5efa\u4e00\u4e2a\u533f\u540d\u51fd\u6570\uff1f\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01,\u4f7f\u7528\u533f\u540d\u51fd\u6570\u521b\u5efa\u95ed\u5305\uff0c\u5176\u8bed\u6cd5\u4e3a\uff1afunc() { \/\/ \u51fd\u6570\u4f53 }\u3002\u95ed\u5305\u5305\u542b\u4e00\u4e2a\u51fd\u6570\u53ca\u5176\u5468\u56f4\u4f5c\u7528\u57df\u53d8\u91cf\uff0c\u5141\u8bb8\u51fd\u6570\u8bbf\u95ee\u5916\u90e8\u53d8\u91cf\uff0c\u5373\u4f7f\u5b83\u5728\u521b\u5efa\u65f6\u5df2\u79bb\u5f00\u5916\u90e8\u4f5c\u7528\u57df\u3002\u5728 Go \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u533f\u540d\u51fd\u6570\u521b\u5efa\u95ed\u5305\uff0c\u4f8b\u5982\uff1afunc() { fmt.Println(x) }\uff08\u5176\u4e2d x \u662f\u5916\u90e8\u53d8\u91cf\uff09\u3002\u95ed\u5305\u53ef\u4ee5\u7528\u4e8e\u5404\u79cd\u573a\u666f\uff0c\u4f8b\u5982\u521b\u5efa\u8ba1\u7b97\u6570\u7ec4\u603b\u548c\u7684\u51fd\u6570\uff1afunc() int { total := 0; for _, v := range arr { total += v }; return total }\uff08\u5176\u4e2d arr \u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u800c total \u662f\u4e00\u4e2a\u7d2f\u52a0\u53d8\u91cf\uff09\u3002,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1729995870671da45e37220.jpg\" class=\"aligncenter\">,\u95ed\u5305\u662f\u7531\u51fd\u6570\u53ca\u5176\u5468\u56f4\u4f5c\u7528\u57df\u53d8\u91cf\u7ec4\u6210\u7684\u3002\u5b83\u5141\u8bb8\u51fd\u6570\u8bbf\u95ee\u5e76\u64cd\u4f5c\u5916\u90e8\u4f5c\u7528\u57df\u53d8\u91cf\uff0c\u5373\u4f7f\u8be5\u51fd\u6570\u5728\u521b\u5efa\u4e4b\u5904\u79bb\u5f00\u4e86\u5916\u90e8\u4f5c\u7528\u57df\u3002\u5728 Go \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u533f\u540d\u51fd\u6570\u521b\u5efa\u95ed\u5305\u3002,\u533f\u540d\u51fd\u6570\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a,\u8ba9\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u95ed\u5305\uff0c\u5176\u4e2d\u533f\u540d\u51fd\u6570\u8bbf\u95ee\u5916\u90e8\u53d8\u91cf<br \/>\n<code>x<\/code>:,\u8f93\u51fa\uff1a,\u8003\u8651\u4e00\u4e2a\u793a\u4f8b\uff0c\u5176\u4e2d\u6211\u4eec\u9700\u8981\u4e00\u4e2a\u51fd\u6570\u6765\u8ba1\u7b97\u4e00\u4e2a\u6570\u7ec4\u7684\u603b\u548c\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u95ed\u5305\u6765\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a\u7d2f\u52a0\u53d8\u91cf\uff1a,\u8f93\u51fa\uff1a,\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8eGOLANG\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~,\u95ed\u5305\u53ef\u4ee5\u6355\u83b7\u5916\u90e8\u4f5c\u7528\u57df\u4e2d\u4efb\u4f55\u53ef\u8bbf\u95ee\u7684\u53d8\u91cf\u3002,\u95ed\u5305\u4e2d\u7684\u53d8\u91cf\u5728\u533f\u540d\u51fd\u6570\u7684\u6240\u6709\u8c03\u7528\u4e2d\u4fdd\u6301\u4e0d\u53d8\u3002,\u4f7f\u7528\u95ed\u5305\u65f6\u8981\u5c0f\u5fc3\uff0c\u56e0\u4e3a\u5b83\u4eec\u53ef\u4ee5\u5bfc\u81f4\u610f\u5916\u7684\u526f\u4f5c\u7528\u3002,\u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762GOLANG\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2aGOLANG\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300a\u5982\u4f55\u4f7f\u7528\u95ed\u5305\u5728 GOLANG \u4e2d\u521b\u5efa\u4e00\u4e2a\u533f\u540d\u51fd\u6570\uff1f\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01,<br \/>\n<b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>\u5982\u4f55\u4f7f\u7528\u95ed\u5305\u5728 GOLANG \u4e2d\u521b\u5efa\u4e00\u4e2a\u533f\u540d\u51fd\u6570\uff1f<\/span><\/p>\n<h1>\u5982\u4f55\u4f7f\u7528\u95ed\u5305\u5728 GOLANG \u4e2d\u521b\u5efa\u4e00\u4e2a\u533f\u540d\u51fd\u6570\uff1f<\/h1>\n<p><span>2024-10-27 10:24:45<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762GOLANG\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2aGOLANG\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300a\u5982\u4f55\u4f7f\u7528\u95ed\u5305\u5728 GOLANG \u4e2d\u521b\u5efa\u4e00\u4e2a\u533f\u540d\u51fd\u6570\uff1f\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01<\/p>\n<p>\u4f7f\u7528\u533f\u540d\u51fd\u6570\u521b\u5efa\u95ed\u5305\uff0c\u5176\u8bed\u6cd5\u4e3a\uff1afunc() { \/\/ \u51fd\u6570\u4f53 }\u3002\u95ed\u5305\u5305\u542b\u4e00\u4e2a\u51fd\u6570\u53ca\u5176\u5468\u56f4\u4f5c\u7528\u57df\u53d8\u91cf\uff0c\u5141\u8bb8\u51fd\u6570\u8bbf\u95ee\u5916\u90e8\u53d8\u91cf\uff0c\u5373\u4f7f\u5b83\u5728\u521b\u5efa\u65f6\u5df2\u79bb\u5f00\u5916\u90e8\u4f5c\u7528\u57df\u3002\u5728 Go \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u533f\u540d\u51fd\u6570\u521b\u5efa\u95ed\u5305\uff0c\u4f8b\u5982\uff1afunc() { fmt.Println(x) }\uff08\u5176\u4e2d x \u662f\u5916\u90e8\u53d8\u91cf\uff09\u3002\u95ed\u5305\u53ef\u4ee5\u7528\u4e8e\u5404\u79cd\u573a\u666f\uff0c\u4f8b\u5982\u521b\u5efa\u8ba1\u7b97\u6570\u7ec4\u603b\u548c\u7684\u51fd\u6570\uff1afunc() int { total := 0; for _, v := range arr { total += v }; return total }\uff08\u5176\u4e2d arr \u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u800c total \u662f\u4e00\u4e2a\u7d2f\u52a0\u53d8\u91cf\uff09\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1729995870671da45e37220.jpg\" class=\"aligncenter\"><\/p>\n<h2>\u5982\u4f55\u4f7f\u7528\u95ed\u5305\u5728 GOLANG \u4e2d\u521b\u5efa\u4e00\u4e2a\u533f\u540d\u51fd\u6570\uff1f<\/h2>\n<p>\u95ed\u5305\u662f\u7531\u51fd\u6570\u53ca\u5176\u5468\u56f4\u4f5c\u7528\u57df\u53d8\u91cf\u7ec4\u6210\u7684\u3002\u5b83\u5141\u8bb8\u51fd\u6570\u8bbf\u95ee\u5e76\u64cd\u4f5c\u5916\u90e8\u4f5c\u7528\u57df\u53d8\u91cf\uff0c\u5373\u4f7f\u8be5\u51fd\u6570\u5728\u521b\u5efa\u4e4b\u5904\u79bb\u5f00\u4e86\u5916\u90e8\u4f5c\u7528\u57df\u3002\u5728 Go \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u533f\u540d\u51fd\u6570\u521b\u5efa\u95ed\u5305\u3002<\/p>\n<h3>\u4f7f\u7528\u533f\u540d\u51fd\u6570\u521b\u5efa\u95ed\u5305<\/h3>\n<p>\u533f\u540d\u51fd\u6570\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>func() {\n    \/\/ \u51fd\u6570\u4f53\n}<\/pre>\n<p>\u8ba9\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u95ed\u5305\uff0c\u5176\u4e2d\u533f\u540d\u51fd\u6570\u8bbf\u95ee\u5916\u90e8\u53d8\u91cf <code>x<\/code>:<\/p>\n<pre>func main() {\n    x := 10\n    f := func() {\n        fmt.Println(x)\n    }\n    f()\n}<\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>10<\/pre>\n<h3>\u5b9e\u6218\u6848\u4f8b<\/h3>\n<p>\u8003\u8651\u4e00\u4e2a\u793a\u4f8b\uff0c\u5176\u4e2d\u6211\u4eec\u9700\u8981\u4e00\u4e2a\u51fd\u6570\u6765\u8ba1\u7b97\u4e00\u4e2a\u6570\u7ec4\u7684\u603b\u548c\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u95ed\u5305\u6765\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a\u7d2f\u52a0\u53d8\u91cf\uff1a<\/p>\n<pre>func main() {\n    arr := []int{1, 2, 3, 4, 5}\n    sum := func() int {\n        total := 0\n        for _, v := range arr {\n            total += v\n        }\n        return total\n    }\n    fmt.Println(sum())\n}<\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>15<\/pre>\n<h3>\u6ce8\u610f\uff1a<\/h3>\n<ul>\n<li>\u95ed\u5305\u4e2d\u7684\u53d8\u91cf\u5728\u533f\u540d\u51fd\u6570\u7684\u6240\u6709\u8c03\u7528\u4e2d\u4fdd\u6301\u4e0d\u53d8\u3002<\/li>\n<li>\u95ed\u5305\u53ef\u4ee5\u6355\u83b7\u5916\u90e8\u4f5c\u7528\u57df\u4e2d\u4efb\u4f55\u53ef\u8bbf\u95ee\u7684\u53d8\u91cf\u3002<\/li>\n<li>\u4f7f\u7528\u95ed\u5305\u65f6\u8981\u5c0f\u5fc3\uff0c\u56e0\u4e3a\u5b83\u4eec\u53ef\u4ee5\u5bfc\u81f4\u610f\u5916\u7684\u526f\u4f5c\u7528\u3002<\/li>\n<\/ul>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8eGOLANG\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   golang\u6846\u67b6\u4ee3\u7801\u751f\u6210\u5668\u6280\u672f\u7684\u53d1\u5c55\u8d8b\u52bf\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   Maven \u4f7f\u7528\u963f\u91cc\u955c\u50cf\u4e0b\u8f7d\u4f9d\u8d56\u901f\u5ea6\u4f9d\u65e7\u7f13\u6162\u600e\u4e48\u529e\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd&#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-205774","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205774","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=205774"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205774\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205774"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205774"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205774"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}