{"id":208209,"date":"2025-07-08T16:46:54","date_gmt":"2025-07-08T08:46:54","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208209\/"},"modified":"2025-07-08T16:46:54","modified_gmt":"2025-07-08T08:46:54","slug":"%e5%a6%82%e4%bd%95%e5%b0%86%e5%88%87%e7%89%87%e8%bd%ac%e6%8d%a2%e4%b8%ba%e5%9b%ba%e5%ae%9a%e9%95%bf%e5%ba%a6%e7%9a%84%e5%88%87%e7%89%87%e5%b9%b6%e8%bf%94%e5%9b%9e","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208209\/","title":{"rendered":"\u5982\u4f55\u5c06\u5207\u7247\u8f6c\u6362\u4e3a\u56fa\u5b9a\u957f\u5ea6\u7684\u5207\u7247\u5e76\u8fd4\u56de"},"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>\u5982\u4f55\u5c06\u5207\u7247\u8f6c\u6362\u4e3a\u56fa\u5b9a\u957f\u5ea6\u7684\u5207\u7247\u5e76\u8fd4\u56de<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-30 11:51:29<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929\u5c31\u6574\u7406\u5206\u4eab\u300a\u5982\u4f55\u5c06\u5207\u7247\u8f6c\u6362\u4e3a\u56fa\u5b9a\u957f\u5ea6\u7684\u5207\u7247\u5e76\u8fd4\u56de\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9Golang\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5982\u4f55\u5c06 []int \u8f6c\u6362\u4e3a [3]int\uff1f<\/p>\n<p>\u8fd9\u4e9b\u90fd\u4e0d\u8d77\u4f5c\u7528\uff1a<\/p>\n<pre>vec := []int{1, 2, 3}\n    \n    t1 := [3]int(vec)\n    t2 := [3]int(vec[:])\n    \/\/cannot convert vec (variable of type []int) to [3]int\n    \n    t3 := [3]int{}\n    copy(t3, vec)\n    \/\/invalid argument: copy expects slice arguments; found t3 (variable of type [3]int) and vec \n    \/\/(value of type []int)<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u8fd9\u91cc\u6709\u4e00\u4e2a \uff0c\u53ef\u4ee5\u66f4\u6e05\u695a\u5730\u8bf4\u660e <code>copy(t3[:],vec)<\/code> \u53d1\u751f\u4e86\u4ec0\u4e48\u3002<\/p>\n<p>go \u6f14\u793a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n)\n\nfunc main() {\n    slice := []int{1, 2, 3, 4}\n    var array [3]int\n    arrayAsSlice := array[:]      \/\/ arrayAsSlice storage IS array; they are aliased.\n    copy(arrayAsSlice, slice[:3]) \/\/ copy into arrayAsSlice modifies array, too.\n    arrayAsSlice[0] = -1          \/\/ slice and array are STILL aliased\n\n    arrayAsSlice = append(arrayAsSlice, 99) \/\/ slice cannot grow in the memory it has, therefore, it is reallocated.\n    arrayAsSlice[0] = 0                     \/\/ Now slice and array are NOT aliased, so this does not modify the array\n\n    fmt.Printf(\"Hello, playground, %+v\", array)\n}\n<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/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-208209","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208209","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=208209"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208209\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}