{"id":3176,"date":"2025-03-25T23:46:53","date_gmt":"2025-03-25T15:46:53","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3176"},"modified":"2025-03-25T23:46:53","modified_gmt":"2025-03-25T15:46:53","slug":"go-%e8%af%ad%e8%a8%80%e5%87%bd%e6%95%b0%ef%bc%88functions%ef%bc%89%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/25\/go-%e8%af%ad%e8%a8%80%e5%87%bd%e6%95%b0%ef%bc%88functions%ef%bc%89%e8%af%a6%e8%a7%a3\/","title":{"rendered":"Go \u8bed\u8a00\u51fd\u6570\uff08Functions\uff09\u8be6\u89e3"},"content":{"rendered":"\n<p>\u5728 Go \u8bed\u8a00\u4e2d\uff0c\u51fd\u6570\u662f\u7a0b\u5e8f\u7684\u57fa\u672c\u6784\u5efa\u5757\u4e4b\u4e00\u3002\u51fd\u6570\u7528\u4e8e\u5c01\u88c5\u53ef\u91cd\u7528\u7684\u4ee3\u7801\u903b\u8f91\uff0c\u5b83\u6709\u8f93\u5165\uff08\u53c2\u6570\uff09\u3001\u5904\u7406\uff08\u51fd\u6570\u4f53\uff09\u548c\u8f93\u51fa\uff08\u8fd4\u56de\u503c\uff09\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. Go \u8bed\u8a00\u51fd\u6570\u57fa\u672c\u7ed3\u6784<\/strong><\/h2>\n\n\n\n<p>Go \u8bed\u8a00\u7684\u51fd\u6570\u7531\u4ee5\u4e0b\u90e8\u5206\u7ec4\u6210\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u51fd\u6570\u58f0\u660e<\/strong>\uff1a\u5305\u62ec <code>func<\/code> \u5173\u952e\u5b57\u3001\u51fd\u6570\u540d\u3001\u53c2\u6570\u5217\u8868\u3001\u8fd4\u56de\u503c\u5217\u8868\u4ee5\u53ca\u51fd\u6570\u4f53\u3002<\/li>\n\n\n\n<li><strong>\u51fd\u6570\u4f53<\/strong>\uff1a\u5305\u542b\u5b9e\u9645\u7684\u4ee3\u7801\u903b\u8f91\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.1 \u8bed\u6cd5<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>func \u51fd\u6570\u540d(\u53c2\u6570\u5217\u8868) \u8fd4\u56de\u503c\u7c7b\u578b {\n    \/\/ \u51fd\u6570\u4f53\n    return \u8fd4\u56de\u503c\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.2 \u793a\u4f8b<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ \u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570\uff0c\u8ba1\u7b97\u4e24\u4e2a\u6570\u7684\u548c\nfunc add(a int, b int) int {\n    return a + b\n}\n\nfunc main() {\n    result := add(10, 20)  \/\/ \u8c03\u7528\u51fd\u6570\n    fmt.Println(\"\u548c:\", result)\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u548c: 30<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u51fd\u6570\u53c2\u6570<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.1 \u5355\u4e00\u53c2\u6570<\/strong><\/h3>\n\n\n\n<p>\u6bcf\u4e2a\u53c2\u6570\u90fd\u9700\u8981\u6307\u5b9a\u7c7b\u578b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func greet(name string) {\n    fmt.Println(\"Hello, \" + name)\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.2 \u591a\u4e2a\u53c2\u6570<\/strong><\/h3>\n\n\n\n<p>\u591a\u4e2a\u53c2\u6570\u53ef\u4ee5\u4e00\u8d77\u58f0\u660e\uff0c\u53c2\u6570\u7c7b\u578b\u4e0e\u7c7b\u578b\u540d\u79f0\u7528\u9017\u53f7\u5206\u9694\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func add(a int, b int) int {\n    return a + b\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.3 \u53ef\u53d8\u53c2\u6570<\/strong><\/h3>\n\n\n\n<p>Go \u652f\u6301\u53ef\u53d8\u6570\u91cf\u7684\u53c2\u6570\u3002\u4f7f\u7528 <code>...<\/code> \u6765\u5b9a\u4e49\u53ef\u53d8\u53c2\u6570\u3002\u53ef\u53d8\u53c2\u6570\u53ef\u4ee5\u63a5\u6536\u4f20\u5165\u7684\u4efb\u610f\u6570\u91cf\u7684\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func sum(nums ...int) int {\n    total := 0\n    for _, num := range nums {\n        total += num\n    }\n    return total\n}\n\nfunc main() {\n    result := sum(1, 2, 3, 4, 5)\n    fmt.Println(\"\u603b\u548c:\", result)\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u603b\u548c: 15<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. \u51fd\u6570\u8fd4\u56de\u503c<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.1 \u5355\u4e00\u8fd4\u56de\u503c<\/strong><\/h3>\n\n\n\n<p>Go \u8bed\u8a00\u51fd\u6570\u53ef\u4ee5\u6709\u4e00\u4e2a\u8fd4\u56de\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func multiply(a int, b int) int {\n    return a * b\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.2 \u591a\u4e2a\u8fd4\u56de\u503c<\/strong><\/h3>\n\n\n\n<p>Go \u8bed\u8a00\u5141\u8bb8\u51fd\u6570\u8fd4\u56de\u591a\u4e2a\u503c\u3002\u8fd9\u5bf9\u4e8e\u8fd4\u56de\u9519\u8bef\u6216\u8005\u591a\u4e2a\u76f8\u5173\u7684\u503c\u975e\u5e38\u6709\u7528\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func divide(a, b int) (int, int) {\n    return a \/ b, a % b  \/\/ \u8fd4\u56de\u5546\u548c\u4f59\u6570\n}\n\nfunc main() {\n    quotient, remainder := divide(10, 3)\n    fmt.Println(\"\u5546:\", quotient, \"\u4f59\u6570:\", remainder)\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5546: 3 \u4f59\u6570: 1<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.3 \u547d\u540d\u8fd4\u56de\u503c<\/strong><\/h3>\n\n\n\n<p>\u5728 Go \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3a\u8fd4\u56de\u503c\u547d\u540d\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u662f\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u547d\u540d\u7684\u8fd4\u56de\u503c\uff0c\u800c\u65e0\u9700\u5728\u51fd\u6570\u4f53\u4e2d\u663e\u5f0f\u5730\u5199\u51fa <code>return<\/code>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func getDimensions() (width, height int) {\n    width = 10\n    height = 5\n    return  \/\/ \u81ea\u52a8\u8fd4\u56de\u547d\u540d\u7684\u503c\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u51fd\u6570\u4f5c\u4e3a\u503c<\/strong><\/h2>\n\n\n\n<p>\u5728 Go \u8bed\u8a00\u4e2d\uff0c\u51fd\u6570\u662f\u4e00\u7b49\u516c\u6c11\uff0c\u53ef\u4ee5\u5c06\u51fd\u6570\u8d4b\u503c\u7ed9\u53d8\u91cf\uff0c\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5176\u4ed6\u51fd\u6570\uff0c\u751a\u81f3\u4f5c\u4e3a\u8fd4\u56de\u503c\u8fd4\u56de\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.1 \u51fd\u6570\u8d4b\u503c<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc add(a, b int) int {\n    return a + b\n}\n\nfunc main() {\n    var sumFunc func(int, int) int\n    sumFunc = add\n    result := sumFunc(10, 20)  \/\/ \u8c03\u7528\u51fd\u6570\n    fmt.Println(\"\u548c:\", result)\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u548c: 30<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.2 \u51fd\u6570\u4f5c\u4e3a\u53c2\u6570<\/strong><\/h3>\n\n\n\n<p>\u51fd\u6570\u53ef\u4ee5\u4f5c\u4e3a\u5176\u4ed6\u51fd\u6570\u7684\u53c2\u6570\u4f20\u9012\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc calculate(a, b int, operation func(int, int) int) int {\n    return operation(a, b)\n}\n\nfunc add(a, b int) int {\n    return a + b\n}\n\nfunc main() {\n    result := calculate(10, 20, add)  \/\/ \u4f20\u9012\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\n    fmt.Println(\"\u548c:\", result)\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u548c: 30<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.3 \u51fd\u6570\u4f5c\u4e3a\u8fd4\u56de\u503c<\/strong><\/h3>\n\n\n\n<p>Go \u8bed\u8a00\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u53e6\u4e00\u4e2a\u51fd\u6570\u3002\u8fd9\u5728\u5b9e\u73b0\u95ed\u5305\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc multiplyBy(factor int) func(int) int {\n    return func(n int) int {\n        return n * factor\n    }\n}\n\nfunc main() {\n    multiplyBy2 := multiplyBy(2)\n    fmt.Println(\"10 * 2 =\", multiplyBy2(10))  \/\/ \u8c03\u7528\u8fd4\u56de\u7684\u51fd\u6570\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>10 * 2 = 20<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. \u533f\u540d\u51fd\u6570<\/strong><\/h2>\n\n\n\n<p>Go \u652f\u6301\u533f\u540d\u51fd\u6570\uff08\u6ca1\u6709\u540d\u5b57\u7684\u51fd\u6570\uff09\uff0c\u8fd9\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u975e\u5e38\u65b9\u4fbf\uff0c\u5c24\u5176\u662f\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u6216\u8005\u95ed\u5305\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5.1 \u533f\u540d\u51fd\u6570\u793a\u4f8b<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    func() {\n        fmt.Println(\"\u8fd9\u662f\u4e00\u4e2a\u533f\u540d\u51fd\u6570\")\n    }()  \/\/ \u7acb\u5373\u8c03\u7528\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u8fd9\u662f\u4e00\u4e2a\u533f\u540d\u51fd\u6570<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6. \u95ed\u5305\uff08Closure\uff09<\/strong><\/h2>\n\n\n\n<p>\u95ed\u5305\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u5b83\u6355\u83b7\u4e86\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\uff0c\u5373\u4f7f\u5916\u90e8\u51fd\u6570\u5df2\u7ecf\u8fd4\u56de\uff0c\u95ed\u5305\u4f9d\u7136\u80fd\u591f\u8bbf\u95ee\u8fd9\u4e9b\u53d8\u91cf\u3002Go \u4e2d\u7684\u51fd\u6570\u4f5c\u4e3a\u4e00\u7b49\u516c\u6c11\u4f7f\u5f97\u95ed\u5305\u6210\u4e3a\u53ef\u80fd\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6.1 \u95ed\u5305\u793a\u4f8b<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc outer() func() int {\n    counter := 0\n    return func() int {\n        counter++\n        return counter\n    }\n}\n\nfunc main() {\n    counterFunc := outer()\n    fmt.Println(counterFunc()) \/\/ \u8f93\u51fa 1\n    fmt.Println(counterFunc()) \/\/ \u8f93\u51fa 2\n    fmt.Println(counterFunc()) \/\/ \u8f93\u51fa 3\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n2\n3<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7. \u603b\u7ed3<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7279\u6027<\/th><th>\u63cf\u8ff0<\/th><\/tr><\/thead><tbody><tr><td><strong>\u51fd\u6570\u58f0\u660e<\/strong><\/td><td><code>func \u51fd\u6570\u540d(\u53c2\u6570\u5217\u8868) \u8fd4\u56de\u503c\u7c7b\u578b {}<\/code><\/td><\/tr><tr><td><strong>\u8fd4\u56de\u503c<\/strong><\/td><td>\u53ef\u8fd4\u56de\u591a\u4e2a\u503c\uff0c\u5305\u62ec\u547d\u540d\u8fd4\u56de\u503c<\/td><\/tr><tr><td><strong>\u53c2\u6570\u7c7b\u578b<\/strong><\/td><td>\u53ef\u4ee5\u662f\u503c\u7c7b\u578b\u6216\u8005\u5f15\u7528\u7c7b\u578b<\/td><\/tr><tr><td><strong>\u53ef\u53d8\u53c2\u6570<\/strong><\/td><td>\u4f7f\u7528 <code>...<\/code> \u8bed\u6cd5\u6765\u5b9a\u4e49\u53ef\u53d8\u6570\u91cf\u7684\u53c2\u6570<\/td><\/tr><tr><td><strong>\u533f\u540d\u51fd\u6570<\/strong><\/td><td>\u6ca1\u6709\u540d\u5b57\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u7acb\u5373\u8c03\u7528<\/td><\/tr><tr><td><strong>\u51fd\u6570\u4f5c\u4e3a\u503c<\/strong><\/td><td>\u51fd\u6570\u53ef\u4ee5\u8d4b\u503c\u7ed9\u53d8\u91cf\uff0c\u4f20\u9012\u7ed9\u5176\u4ed6\u51fd\u6570\u6216\u4f5c\u4e3a\u8fd4\u56de\u503c<\/td><\/tr><tr><td><strong>\u95ed\u5305<\/strong><\/td><td>\u51fd\u6570\u53ef\u4ee5\u6355\u83b7\u5176\u5916\u90e8\u4f5c\u7528\u57df\u7684\u53d8\u91cf<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Go \u8bed\u8a00\u4e2d\uff0c\u51fd\u6570\u662f\u7a0b\u5e8f\u7684\u57fa\u672c\u6784\u5efa\u5757\u4e4b\u4e00\u3002\u51fd\u6570\u7528\u4e8e\u5c01\u88c5\u53ef\u91cd\u7528\u7684\u4ee3\u7801\u903b\u8f91\uff0c\u5b83\u6709\u8f93\u5165\uff08\u53c2\u6570\uff09\u3001\u5904\u7406\uff08\u51fd\u6570\u4f53\uff09\u548c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3177,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58],"tags":[],"class_list":["post-3176","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-golang"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3176","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3176"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3176\/revisions"}],"predecessor-version":[{"id":3178,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3176\/revisions\/3178"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3177"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}