{"id":10021013,"date":"2025-09-24T18:26:15","date_gmt":"2025-09-24T10:26:15","guid":{"rendered":"https:\/\/itmax.cn\/?p=10021013"},"modified":"2025-09-24T18:26:15","modified_gmt":"2025-09-24T10:26:15","slug":"golang%e6%a8%a1%e6%8b%9f%e9%87%8d%e8%bd%bd%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10021013","title":{"rendered":"golang\u6a21\u62df\u91cd\u8f7d\u65b9\u6cd5"},"content":{"rendered":"<p>Golang \u7684\u8bbe\u8ba1\u54f2\u5b66\u5f3a\u8c03\u7b80\u6d01\u548c\u660e\u786e\uff0c\u56e0\u6b64\u5b83\u5e76\u4e0d\u76f4\u63a5\u652f\u6301\u4f20\u7edf\u610f\u4e49\u4e0a\u7684\u51fd\u6570\u6216\u65b9\u6cd5\u91cd\u8f7d\uff08\u5373\u540c\u4e00\u4f5c\u7528\u57df\u5185\u540c\u540d\u4f46\u53c2\u6570\u7c7b\u578b\u6216\u6570\u91cf\u4e0d\u540c\u7684\u591a\u4e2a\u51fd\u6570\uff09\u3002\u4e0d\u8fc7\uff0c\u4f60\u4ecd\u7136\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u60ef\u7528\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd\u3002\u4e0b\u9762\u4e3a\u4f60\u4ecb\u7ecd\u51e0\u79cd\u4e3b\u6d41\u7684\u65b9\u6cd5\u3002<\/p>\n<p>\u65b9\u6cd5 \u6838\u5fc3\u601d\u60f3 \u9002\u7528\u573a\u666f \u4f18\u70b9 \u7f3a\u70b9<\/p>\n<p>\u4e0d\u540c\u51fd\u6570\u540d \u4e3a\u529f\u80fd\u76f8\u4f3c\u4f46\u53c2\u6570\u4e0d\u540c\u7684\u51fd\u6570\u8d4b\u4e88\u6e05\u6670\u4e14\u4e0d\u540c\u7684\u540d\u79f0 \u6240\u6709\u573a\u666f\uff0c\u5c24\u5176\u662f\u53c2\u6570\u7c7b\u578b\u548c\u6570\u91cf\u660e\u786e\u4e14\u56fa\u5b9a\u65f6 \u4ee3\u7801\u610f\u56fe\u6e05\u6670\u660e\u4e86\uff0c\u7f16\u8bd1\u65f6\u7c7b\u578b\u5b89\u5168\uff0c\u6700\u7b26\u5408 Go \u98ce\u683c \u51fd\u6570\u6570\u91cf\u8f83\u591a\u65f6\uff0c\u9700\u8981\u8d77\u4e0d\u540c\u7684\u540d\u5b57<\/p>\n<p>\u63a5\u53e3\u4e0e\u7c7b\u578b\u65ad\u8a00 \u4f7f\u7528\u7a7a\u63a5\u53e3 interface{} \u63a5\u6536\u4efb\u610f\u7c7b\u578b\uff0c\u5728\u51fd\u6570\u5185\u90e8\u901a\u8fc7\u7c7b\u578b\u65ad\u8a00\u6216\u7c7b\u578b\u5207\u6362\u6765\u533a\u5206\u5904\u7406 \u53c2\u6570\u7c7b\u578b\u65e0\u6cd5\u5728\u7f16\u8bd1\u65f6\u786e\u5b9a\uff0c\u9700\u8981\u5904\u7406\u591a\u79cd\u672a\u77e5\u7c7b\u578b\u65f6 \u7075\u6d3b\u6027\u9ad8\uff0c\u53ef\u4ee5\u5904\u7406\u8fd0\u884c\u65f6\u624d\u786e\u5b9a\u7684\u7c7b\u578b \u727a\u7272\u7f16\u8bd1\u65f6\u7c7b\u578b\u5b89\u5168\uff0c\u9519\u8bef\u5728\u8fd0\u884c\u65f6\u624d\u80fd\u53d1\u73b0\uff0c\u4ee3\u7801\u7a0d\u663e\u590d\u6742<\/p>\n<p>\u53ef\u53d8\u53c2\u6570 \u4f7f\u7528 &#8230;T \u8bed\u6cd5\u63a5\u6536\u6570\u91cf\u4e0d\u5b9a\u7684\u540c\u7c7b\u578b\u53c2\u6570 \u9700\u8981\u5904\u7406\u6570\u91cf\u4e0d\u5b9a\u4f46\u7c7b\u578b\u76f8\u540c\u7684\u53c2\u6570\u65f6 \u7b80\u5316\u51fd\u6570\u8c03\u7528\uff0c\u652f\u6301\u4efb\u610f\u6570\u91cf\u7684\u53c2\u6570 \u901a\u5e38\u4ec5\u9650\u4e8e\u540c\u4e00\u7c7b\u578b\u53c2\u6570<\/p>\n<p>\u51fd\u6570\u9009\u9879\u6a21\u5f0f \u4f7f\u7528\u9ad8\u9636\u51fd\u6570\u6765\u914d\u7f6e\u4e00\u4e2a\u5bf9\u8c61\uff0c\u901a\u5e38\u7528\u4e8e\u5904\u7406\u591a\u4e2a\u53ef\u9009\u53c2\u6570 \u6784\u9020\u51fd\u6570\u6216\u914d\u7f6e\u51fd\u6570\u6709\u5927\u91cf\u53ef\u9009\u53c2\u6570\uff0c\u4e14\u5e0c\u671b\u4ee3\u7801\u4f18\u96c5\u3001\u53ef\u6269\u5c55\u6027\u5f3a\u65f6 \u9ad8\u5ea6\u53ef\u6269\u5c55\uff0c\u4ee3\u7801\u53ef\u8bfb\u6027\u597d\uff0c\u975e\u5e38\u4f18\u96c5\u7684 Go \u60ef\u7528\u6cd5 \u5b9e\u73b0\u8d77\u6765\u76f8\u5bf9\u590d\u6742\u4e00\u4e9b<\/p>\n<p>\ud83d\udee0\ufe0f \u4e0d\u540c\u51fd\u6570\u540d\uff08\u6700\u63a8\u8350\uff09<\/p>\n<p>\u8fd9\u662f\u6700\u76f4\u63a5\u3001\u6700\u7b26\u5408 Go \u8bed\u8a00\u98ce\u683c\u7684\u65b9\u5f0f\u3002\u901a\u8fc7\u4e3a\u4e0d\u540c\u53c2\u6570\u7c7b\u578b\u7684\u51fd\u6570\u8d4b\u4e88\u4e0d\u540c\u7684\u540d\u79f0\uff0c\u4f7f\u4ee3\u7801\u610f\u56fe\u4e00\u76ee\u4e86\u7136\u3002<\/p>\n<pre><code>package main\n\nimport &quot;fmt&quot;\n\n\/\/ \u5904\u7406\u6574\u6570\u52a0\u6cd5\nfunc AddInt(a, b int) int {\n    return a + b\n}\n\n\/\/ \u5904\u7406\u6d6e\u70b9\u6570\u52a0\u6cd5\nfunc AddFloat(a, b float64) float64 {\n    return a + b\n}\n\n\/\/ \u5904\u7406\u5b57\u7b26\u4e32\u62fc\u63a5\nfunc ConcatString(s1, s2 string) string {\n    return s1 + s2\n}\n\nfunc main() {\n    sumInt := AddInt(1, 2)\n    sumFloat := AddFloat(1.5, 2.5)\n    str := ConcatString(&quot;Hello, &quot;, &quot;World!&quot;)\n    \n    fmt.Println(sumInt)    \/\/ \u8f93\u51fa: 3\n    fmt.Println(sumFloat)  \/\/ \u8f93\u51fa: 4\n    fmt.Println(str)       \/\/ \u8f93\u51fa: Hello, World!\n}<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u597d\u5904\u662f\u7c7b\u578b\u5b89\u5168\uff0c\u7f16\u8bd1\u5668\u5c31\u80fd\u5e2e\u4f60\u68c0\u67e5\u9519\u8bef\uff0c\u800c\u4e14\u4ee3\u7801\u975e\u5e38\u6e05\u6670\u3002<\/p>\n<p>\ud83d\udee0\ufe0f \u63a5\u53e3\u4e0e\u7c7b\u578b\u65ad\u8a00<\/p>\n<p>\u5f53\u4f60\u9700\u8981\u5904\u7406\u7684\u53c2\u6570\u7c7b\u578b\u5728\u7f16\u8bd1\u671f\u65e0\u6cd5\u786e\u5b9a\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u7a7a\u63a5\u53e3 interface{}\uff08\u5728 Go 1.18 \u4e4b\u540e\uff0c\u6cdb\u578b\u901a\u5e38\u662f\u66f4\u597d\u7684\u9009\u62e9\uff09\u914d\u5408\u7c7b\u578b\u65ad\u8a00\u3002<\/p>\n<pre><code class=\"go\">package main\n\nimport &quot;fmt&quot;\n\nfunc ProcessInput(input interface{}) {\n    switch v := input.(type) { \/\/ \u7c7b\u578b\u5f00\u5173 (type switch)\n    case int:\n        fmt.Printf(&quot;\u5904\u7406\u6574\u6570: %d\/n&quot;, v)\n    case string:\n        fmt.Printf(&quot;\u5904\u7406\u5b57\u7b26\u4e32: %s\/n&quot;, v)\n    case float64:\n        fmt.Printf(&quot;\u5904\u7406\u6d6e\u70b9\u6570: %f\/n&quot;, v)\n    default:\n        fmt.Printf(&quot;\u4e0d\u652f\u6301\u7684\u7c7b\u578b: %T\/n&quot;, v)\n    }\n}\n\nfunc main() {\n    ProcessInput(42)\n    ProcessInput(&quot;hello&quot;)\n    ProcessInput(3.14)\n    ProcessInput([]int{})\n}\n\/\/ \u8f93\u51fa:\n\/\/ \u5904\u7406\u6574\u6570: 42\n\/\/ \u5904\u7406\u5b57\u7b26\u4e32: hello\n\/\/ \u5904\u7406\u6d6e\u70b9\u6570: 3.140000\n\/\/ \u4e0d\u652f\u6301\u7684\u7c7b\u578b: []int<\/code><\/pre>\n<p>\u6ce8\u610f\uff1a\u8fd9\u79cd\u65b9\u5f0f\u4f1a\u5931\u53bb\u7f16\u8bd1\u65f6\u7684\u7c7b\u578b\u5b89\u5168\uff0c\u5982\u679c\u4f20\u5165\u672a\u5904\u7406\u7684\u7c7b\u578b\uff0c\u9519\u8bef\u8981\u5230\u8fd0\u884c\u65f6\u624d\u4f1a\u53d1\u73b0\u3002<\/p>\n<p>\ud83d\udee0\ufe0f \u53ef\u53d8\u53c2\u6570 (Variadic Functions)<\/p>\n<p>\u5982\u679c\u4f60\u7684\u51fd\u6570\u53ea\u662f\u9700\u8981\u5904\u7406\u6570\u91cf\u4e0d\u5b9a\u4f46\u7c7b\u578b\u76f8\u540c\u7684\u53c2\u6570\uff0c\u53ef\u53d8\u53c2\u6570\u975e\u5e38\u5408\u9002\u3002<\/p>\n<pre><code>package main\n\nimport &quot;fmt&quot;\n\n\/\/ ...int \u8868\u793a\u53ef\u4ee5\u63a5\u53d7\u4efb\u610f\u6570\u91cf\u7684 int \u578b\u53c2\u6570\nfunc SumNumbers(numbers ...int) int {\n    total := 0\n    for _, num := range numbers {\n        total += num\n    }\n    return total\n}\n\nfunc main() {\n    \/\/ \u53ef\u4ee5\u4f20\u4efb\u610f\u6570\u91cf\u7684\u53c2\u6570\n    sum1 := SumNumbers(1, 2, 3)\n    sum2 := SumNumbers(1, 2, 3, 4, 5)\n    \n    \/\/ \u4e5f\u53ef\u4ee5\u4f20\u9012\u4e00\u4e2a\u5207\u7247\uff0c\u540e\u9762\u8981\u52a0...\n    nums := []int{10, 20, 30}\n    sum3 := SumNumbers(nums...)\n    \n    fmt.Println(sum1) \/\/ \u8f93\u51fa: 6\n    fmt.Println(sum2) \/\/ \u8f93\u51fa: 15\n    fmt.Println(sum3) \/\/ \u8f93\u51fa: 60\n}\n\n\n\ud83d\udee0\ufe0f \u51fd\u6570\u9009\u9879\u6a21\u5f0f (Functional Options Pattern)\n\n\u8fd9\u662f\u4e00\u79cd\u975e\u5e38\u4f18\u96c5\u7684\u5904\u7406\u591a\u4e2a\u53ef\u9009\u53c2\u6570\u7684\u65b9\u6cd5\uff0c\u5e38\u7528\u4e8e\u6784\u9020\u590d\u6742\u5bf9\u8c61\u6216\u914d\u7f6e\u3002\npackage main\n\nimport (\n    &quot;fmt&quot;\n    &quot;time&quot;\n)\n\ntype ServerConfig struct {\n    Port        int\n    Timeout     time.Duration\n    MaxConnects int\n    Debug       bool\n}\n\n\/\/ Option \u662f\u4e00\u4e2a\u51fd\u6570\u7c7b\u578b\uff0c\u5b83\u63a5\u6536\u4e00\u4e2a\u6307\u5411ServerConfig\u7684\u6307\u9488\ntype Option func(*ServerConfig)\n\n\/\/ \u4e0b\u9762\u662f\u4e00\u4e9b\u8bbe\u7f6e\u9009\u9879\u7684\u51fd\u6570\uff0c\u5b83\u4eec\u8fd4\u56deOption\u51fd\u6570\n\nfunc WithPort(port int) Option {\n    return func(c *ServerConfig) {\n        c.Port = port\n    }\n}\n\nfunc WithTimeout(timeout time.Duration) Option {\n    return func(c *ServerConfig) {\n        c.Timeout = timeout\n    }\n}\n\nfunc WithMaxConnects(max int) Option {\n    return func(c *ServerConfig) {\n        c.MaxConnects = max\n    }\n}\n\nfunc WithDebug(debug bool) Option {\n    return func(c *ServerConfig) {\n        c.Debug = debug\n    }\n}\n\n\/\/ NewServer \u662f\u6784\u9020\u51fd\u6570\uff0c\u63a5\u53d7\u4efb\u610f\u6570\u91cf\u7684Option\u51fd\u6570\nfunc NewServer(options ...Option) *ServerConfig {\n    config := &amp;ServerConfig{\n        Port:        8080,    \/\/ \u9ed8\u8ba4\u503c\n        Timeout:     time.Second * 30,\n        MaxConnects: 100,\n        Debug:       false,\n    }\n    \n    \/\/ \u5e94\u7528\u6240\u6709\u9009\u9879\n    for _, option := range options {\n        option(config)\n    }\n    \n    return config\n}\n\nfunc main() {\n    \/\/ \u4f7f\u7528\u9ed8\u8ba4\u914d\u7f6e\n    defaultServer := NewServer()\n    fmt.Printf(&quot;Default: %+v\/n&quot;, defaultServer)\n    \n    \/\/ \u4f7f\u7528\u81ea\u5b9a\u4e49\u914d\u7f6e\uff0c\u53ef\u9009\u53c2\u6570\u4e14\u987a\u5e8f\u4efb\u610f\n    customServer := NewServer(\n        WithPort(9000),\n        WithTimeout(time.Minute),\n        WithDebug(true),\n    )\n    fmt.Printf(&quot;Custom: %+v\/n&quot;, customServer)\n}<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u8bfb\u6027\u975e\u5e38\u597d\uff0c\u800c\u4e14\u9ad8\u5ea6\u53ef\u6269\u5c55\uff0c\u65b0\u589e\u53ef\u9009\u53c2\u6570\u53ea\u9700\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684 WithXxx \u51fd\u6570\u5373\u53ef\uff0c\u65e0\u9700\u4fee\u6539 NewServer \u51fd\u6570\u7684\u7b7e\u540d\u3002<\/p>\n<p>\ud83d\udca1 \u5982\u4f55\u9009\u62e9<\/p>\n<p>\u2022   \u5bf9\u4e8e\u53c2\u6570\u7c7b\u578b\u548c\u6570\u91cf\u660e\u786e\u7684\u60c5\u51b5\uff0c\u4f18\u5148\u9009\u62e9\u4e0d\u540c\u51fd\u6570\u540d\u3002<\/p>\n<p>\u2022   \u5982\u679c\u9700\u8981\u5904\u7406\u8fd0\u884c\u65f6\u624d\u786e\u5b9a\u7684\u7c7b\u578b\uff0c\u8003\u8651\u4f7f\u7528\u63a5\u53e3\u4e0e\u7c7b\u578b\u65ad\u8a00\uff08\u6216\u6cdb\u578b\uff09\u3002<\/p>\n<p>\u2022   \u5982\u679c\u53ea\u662f\u53c2\u6570\u6570\u91cf\u4e0d\u5b9a\u4f46\u7c7b\u578b\u76f8\u540c\uff0c\u4f7f\u7528\u53ef\u53d8\u53c2\u6570\u3002<\/p>\n<p>\u2022   \u5982\u679c\u51fd\u6570\uff08\u5c24\u5176\u662f\u6784\u9020\u51fd\u6570\uff09\u6709\u5927\u91cf\u53ef\u9009\u53c2\u6570\uff0c\u5e76\u4e14\u5e0c\u671b\u914d\u7f6e\u4ee3\u7801\u6e05\u6670\u3001\u6613\u4e8e\u6269\u5c55\uff0c\u51fd\u6570\u9009\u9879\u6a21\u5f0f\u662f\u6700\u4f18\u96c5\u7684\u9009\u62e9\u3002<\/p>\n<p>\u26a0\ufe0f \u6ce8\u610f\u4e8b\u9879<\/p>\n<p>\u2022   \u7c7b\u578b\u5b89\u5168\uff1a\u4f18\u5148\u9009\u62e9\u80fd\u5728\u7f16\u8bd1\u671f\u68c0\u67e5\u7c7b\u578b\u7684\u65b9\u6cd5\uff08\u5982\u4e0d\u540c\u51fd\u6570\u540d\u3001\u6cdb\u578b\uff09\u3002<\/p>\n<p>\u2022   \u4ee3\u7801\u6e05\u6670\u5ea6\uff1aGo \u975e\u5e38\u91cd\u89c6\u4ee3\u7801\u7684\u6e05\u6670\u548c\u53ef\u8bfb\u6027\uff0c\u907f\u514d\u4f7f\u7528\u8fc7\u4e8e\u590d\u6742\u6216\u6666\u6da9\u7684\u6a21\u62df\u91cd\u8f7d\u6280\u5de7\u3002<\/p>\n<p>\u2022   \u9519\u8bef\u5904\u7406\uff1a\u4f7f\u7528\u7c7b\u578b\u65ad\u8a00\u6216\u7a7a\u63a5\u53e3\u65f6\uff0c\u522b\u5fd8\u4e86\u5904\u7406\u672a\u77e5\u7c7b\u578b\u6216\u9519\u8bef\u60c5\u51b5\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000047287284\">https:\/\/segmentfault.com\/a\/1190000047287284<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Golang \u7684\u8bbe\u8ba1\u54f2\u5b66\u5f3a\u8c03\u7b80\u6d01\u548c\u660e\u786e\uff0c\u56e0\u6b64\u5b83\u5e76\u4e0d\u76f4\u63a5\u652f\u6301\u4f20\u7edf\u610f\u4e49\u4e0a\u7684\u51fd\u6570\u6216\u65b9\u6cd5\u91cd\u8f7d\uff08\u5373\u540c\u4e00\u4f5c\u7528\u57df\u5185\u540c\u540d\u4f46\u53c2\u6570 [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10021013","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021013","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10021013"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021013\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10021013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10021013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10021013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}