{"id":205849,"date":"2025-06-12T12:34:58","date_gmt":"2025-06-12T04:34:58","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205849\/"},"modified":"2025-06-12T12:34:58","modified_gmt":"2025-06-12T04:34:58","slug":"%e5%a6%82%e4%bd%95%e7%94%a8-go-%e7%9a%84%e5%8c%bf%e5%90%8d%e5%87%bd%e6%95%b0%e6%8d%95%e8%8e%b7%e5%8f%98%e9%87%8f%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205849\/","title":{"rendered":"\u5982\u4f55\u7528 Go \u7684\u533f\u540d\u51fd\u6570\u6355\u83b7\u53d8\u91cf\uff1f"},"content":{"rendered":"<p>\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728GOLANG\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300a\u5982\u4f55\u7528 Go \u7684\u533f\u540d\u51fd\u6570\u6355\u83b7\u53d8\u91cf\uff1f\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730018263671dfbd726cfe.jpg\" class=\"aligncenter\">,<br \/>\n<strong>\u5982\u4f55\u7528 Go \u7684\u533f\u540d\u51fd\u6570\u6355\u83b7\u53d8\u91cf\uff1f<\/strong>,\u5728 Go \u4e2d\uff0c\u533f\u540d\u51fd\u6570\uff08\u4e5f\u79f0\u4e3a lambda \u8868\u8fbe\u5f0f\uff09\u53ef\u4ee5\u6355\u83b7\u5916\u90e8\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a\u95ed\u5305\u3002\u8fd9\u5141\u8bb8\u533f\u540d\u51fd\u6570\u4f7f\u7528\u5e76\u4fee\u6539\u5916\u90e8\u53d8\u91cf\uff0c\u5373\u4f7f\u5728\u5916\u90e8\u51fd\u6570\u8fd4\u56de\u540e\u4e5f\u662f\u5982\u6b64\u3002,\u8981\u6355\u83b7\u53d8\u91cf\uff0c\u53ef\u4ee5\u5728\u533f\u540d\u51fd\u6570\u4e2d\u4f7f\u7528<br \/>\n<code>func<\/code> \u5173\u952e\u5b57\u548c\u53d8\u91cf\u540d\uff0c\u5982\u4e0b\u6240\u793a\uff1a,<br \/>\n<strong>\u5b9e\u6218\u6848\u4f8b<\/strong>,\u8003\u8651\u4ee5\u4e0b\u573a\u666f\uff1a\u6211\u4eec\u6709\u4e00\u4e2a<br \/>\n<code>calculateTotal<\/code> \u51fd\u6570\uff0c\u8ba1\u7b97\u591a\u4e2a\u6570\u5b57\u7684\u603b\u548c\u3002\u6211\u4eec\u60f3\u521b\u5efa\u4e00\u4e2a\u533f\u540d\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u901a\u8fc7\u95ed\u5305\u6355\u83b7<br \/>\n<code>total<\/code> \u53d8\u91cf\u5e76\u968f\u7740\u65b0\u6570\u5b57\u7684\u6dfb\u52a0\u5b9e\u65f6\u66f4\u65b0\u603b\u548c\u3002,\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<br \/>\n<code>addNumbers<\/code> \u533f\u540d\u51fd\u6570\u6355\u83b7\u4e86<br \/>\n<code>calculateTotal<\/code> \u51fd\u6570\u4e2d\u7684<br \/>\n<code>total<\/code> \u53d8\u91cf\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u6bcf\u6b21\u8c03\u7528\u65f6\u4fee\u6539\u548c\u6253\u5370\u6b64\u53d8\u91cf\u7684\u503c\u3002,\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u7528 Go \u7684\u533f\u540d\u51fd\u6570\u6355\u83b7\u53d8\u91cf\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728GOLANG\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01,\u5982\u4f55\u7528 Go \u7684\u533f\u540d\u51fd\u6570\u6355\u83b7\u53d8\u91cf\uff1f,\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728GOLANG\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300a\u5982\u4f55\u7528 Go \u7684\u533f\u540d\u51fd\u6570\u6355\u83b7\u53d8\u91cf\uff1f\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~,<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\u7528 Go \u7684\u533f\u540d\u51fd\u6570\u6355\u83b7\u53d8\u91cf\uff1f<\/span><\/p>\n<h1>\u5982\u4f55\u7528 Go \u7684\u533f\u540d\u51fd\u6570\u6355\u83b7\u53d8\u91cf\uff1f<\/h1>\n<p><span>2024-10-27 16:37:58<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728GOLANG\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300a\u5982\u4f55\u7528 Go \u7684\u533f\u540d\u51fd\u6570\u6355\u83b7\u53d8\u91cf\uff1f\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730018263671dfbd726cfe.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u7528 Go \u7684\u533f\u540d\u51fd\u6570\u6355\u83b7\u53d8\u91cf\uff1f<\/strong><\/p>\n<p>\u5728 Go \u4e2d\uff0c\u533f\u540d\u51fd\u6570\uff08\u4e5f\u79f0\u4e3a lambda \u8868\u8fbe\u5f0f\uff09\u53ef\u4ee5\u6355\u83b7\u5916\u90e8\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a\u95ed\u5305\u3002\u8fd9\u5141\u8bb8\u533f\u540d\u51fd\u6570\u4f7f\u7528\u5e76\u4fee\u6539\u5916\u90e8\u53d8\u91cf\uff0c\u5373\u4f7f\u5728\u5916\u90e8\u51fd\u6570\u8fd4\u56de\u540e\u4e5f\u662f\u5982\u6b64\u3002<\/p>\n<p>\u8981\u6355\u83b7\u53d8\u91cf\uff0c\u53ef\u4ee5\u5728\u533f\u540d\u51fd\u6570\u4e2d\u4f7f\u7528 <code>func<\/code> \u5173\u952e\u5b57\u548c\u53d8\u91cf\u540d\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>func(x int) func() {\n    return func() {\n        \/\/ x \u53ef\u4ee5\u88ab\u8bbf\u95ee\u548c\u4fee\u6539\n        x++\n    }\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4ee5\u4e0b\u573a\u666f\uff1a\u6211\u4eec\u6709\u4e00\u4e2a <code>calculateTotal<\/code> \u51fd\u6570\uff0c\u8ba1\u7b97\u591a\u4e2a\u6570\u5b57\u7684\u603b\u548c\u3002\u6211\u4eec\u60f3\u521b\u5efa\u4e00\u4e2a\u533f\u540d\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u901a\u8fc7\u95ed\u5305\u6355\u83b7 <code>total<\/code> \u53d8\u91cf\u5e76\u968f\u7740\u65b0\u6570\u5b57\u7684\u6dfb\u52a0\u5b9e\u65f6\u66f4\u65b0\u603b\u548c\u3002<\/p>\n<pre>package main\n\nimport \"fmt\"\n\nfunc calculateTotal(numbers []int) int {\n    total := 0\n    for _, num := range numbers {\n        total += num\n    }\n    return total\n}\n\nfunc main() {\n    addNumbers := func(additionalNum int) {\n        \/\/ \u6355\u83b7 calculateTotal \u51fd\u6570\u4e2d\u7684 total \u53d8\u91cf\n        total := calculateTotal([]int{1, 2, 3})\n        total += additionalNum\n        fmt.Println(\"Current total:\", total)\n    }\n\n    addNumbers(4)  \/\/ \u8f93\u51fa: Current total: 7\n    addNumbers(6)  \/\/ \u8f93\u51fa: Current total: 13\n}<\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>addNumbers<\/code> \u533f\u540d\u51fd\u6570\u6355\u83b7\u4e86 <code>calculateTotal<\/code> \u51fd\u6570\u4e2d\u7684 <code>total<\/code> \u53d8\u91cf\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u6bcf\u6b21\u8c03\u7528\u65f6\u4fee\u6539\u548c\u6253\u5370\u6b64\u53d8\u91cf\u7684\u503c\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u7528 Go \u7684\u533f\u540d\u51fd\u6570\u6355\u83b7\u53d8\u91cf\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728GOLANG\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   Antd Pagination \u521d\u59cb\u6e32\u67d3\u6837\u5f0f\u9519\u4e71\u8be5\u5982\u4f55\u89e3\u51b3\uff1f\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   Java \u51fd\u6570\u4e2d\u4f7f\u7528 varargs \u53c2\u6570\u7684\u4f18\u70b9\u6709\u54ea\u4e9b\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728G&#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-205849","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205849","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=205849"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205849\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205849"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205849"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205849"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}