{"id":207889,"date":"2025-07-08T14:43:14","date_gmt":"2025-07-08T06:43:14","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207889\/"},"modified":"2025-07-08T14:43:14","modified_gmt":"2025-07-08T06:43:14","slug":"go%e9%94%99%e8%af%af%ef%bc%9aint%e7%b1%bb%e5%9e%8b%e7%9a%84%e5%80%bc%e4%bd%9c%e4%b8%baint%e5%80%bc","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207889\/","title":{"rendered":"Go\u9519\u8bef\uff1aint\u7c7b\u578b\u7684\u503c\u4f5c\u4e3aint\u503c"},"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>Go\u9519\u8bef\uff1aint\u7c7b\u578b\u7684\u503c\u4f5c\u4e3aint\u503c<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-26 14:15:38<\/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\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300aGo\u9519\u8bef\uff1aint\u7c7b\u578b\u7684\u503c\u4f5c\u4e3aint\u503c\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u662f\u7f16\u7a0b\u548c stackoverflow \u7684\u521d\u5b66\u8005\u3002<\/p>\n<p>\u6211\u5fc5\u987b\u5728 go \u4e2d\u521b\u5efa\u4e00\u4e2a\u9012\u5f52\u51fd\u6570\u6765\u6dfb\u52a0\u6570\u7ec4\u7684\u5143\u7d20\uff0c\u5982\u679c\u6570\u7ec4\u7684\u957f\u5ea6\u4e3a 0\uff0c\u5219\u8fd4\u56de 0\u3002<\/p>\n<pre>func Suma(vector []int) int {\n    n := len(vector)\n    if n == 0 {\n        return 0\n    } else {\n        return Suma(vector[n] + vector[n-1])\n    }\n}\n\nfunc main() {\n    fmt.Println(\"Hello, \u4e16\u754c\")\n    vector := []int{1, 2, 3, 4, 5}\n    res := Suma(vector)\n    fmt.Println(res)\n}<\/pre>\n<p>\u5b83\u7ed9\u4e86\u6211\u8fd9\u4e2a\u9519\u8bef\uff0c\u4f46\u6211\u4e0d\u660e\u767d\u3002<\/p>\n<pre><code>\ncannot use vector[n] + vector[n - 1] (value of type int) as []int value in argument to Suma\n<\/code><\/pre>\n<p>\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0\u6b64\u9519\u8bef\u4ee5\u53ca\u5982\u4f55\u4fee\u590d\u5b83\uff1f<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<h2>\u8fd9\u662f\u4f60\u7684\u95ee\u9898\uff1a<\/h2>\n<p>\u60a8\u770b\u5230\u7684\u9519\u8bef\u6d88\u606f\u662f\u56e0\u4e3a\u60a8\u5c1d\u8bd5\u5c06 int \u503c\u4f20\u9012\u7ed9 Suma \u51fd\u6570\uff0c\u8be5\u51fd\u6570\u9700\u8981\u4e00\u4e2a int \u5207\u7247\u3002<\/p>\n<pre>package main\n\nimport \"fmt\"\n\nfunc Suma(vector []int) int {\n    n := len(vector)\n    if n == 0 {\n        return 0\n    } else {\n        \/\/ You should call Suma recursively with a slice of the vector, excluding the last element.\n        \/\/ Also, you need to add the current element (vector[n-1]) to the sum.\n        return vector[n-1] + Suma(vector[:n-1])\n    }\n}\n\nfunc main() {\n    fmt.Println(\"Hello, \u4e16\u754c\")\n    vector := []int{1, 2, 3, 4, 5}\n    res := Suma(vector)\n    fmt.Println(res)\n}\n\n<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aGo\u9519\u8bef\uff1aint\u7c7b\u578b\u7684\u503c\u4f5c\u4e3aint\u503c\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\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-207889","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207889","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=207889"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207889\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207889"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207889"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207889"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}