{"id":3192,"date":"2025-03-25T23:53:24","date_gmt":"2025-03-25T15:53:24","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3192"},"modified":"2025-03-25T23:53:24","modified_gmt":"2025-03-25T15:53:24","slug":"go-%e8%af%ad%e8%a8%80%e5%88%87%e7%89%87%ef%bc%88slice%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/25\/go-%e8%af%ad%e8%a8%80%e5%88%87%e7%89%87%ef%bc%88slice%ef%bc%89\/","title":{"rendered":"Go \u8bed\u8a00\u5207\u7247\uff08Slice\uff09"},"content":{"rendered":"\n<p>\u5207\u7247\uff08<code>slice<\/code>\uff09\u662f Go \u8bed\u8a00\u4e2d\u4e00\u79cd\u91cd\u8981\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5b83\u662f\u4e00\u79cd\u52a8\u6001\u6570\u7ec4\uff0c\u6bd4\u6570\u7ec4\u66f4\u7075\u6d3b\u3002\u4e0e\u6570\u7ec4\u4e0d\u540c\uff0c\u5207\u7247\u7684\u957f\u5ea6\u662f\u53ef\u4ee5\u6539\u53d8\u7684\uff0c\u5141\u8bb8\u9ad8\u6548\u5730\u52a8\u6001\u7ba1\u7406\u6570\u636e\u3002\u5207\u7247\u901a\u5e38\u7528\u4e8e\u64cd\u4f5c\u548c\u5904\u7406\u6570\u636e\u96c6\u5408\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. \u5207\u7247\u7684\u57fa\u672c\u6982\u5ff5<\/strong><\/h2>\n\n\n\n<p>\u5207\u7247\u662f\u5bf9\u6570\u7ec4\u7684\u4e00\u4e2a\u62bd\u8c61\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u7684\u64cd\u4f5c\u65b9\u5f0f\u3002\u5207\u7247\u5e76\u4e0d\u5b58\u50a8\u6570\u636e\uff0c\u800c\u662f\u5f15\u7528\u5e95\u5c42\u6570\u7ec4\u7684\u4e00\u6bb5\u8fde\u7eed\u5185\u5b58\u533a\u57df\u3002\u5207\u7247\u7684\u5bb9\u91cf\u548c\u957f\u5ea6\u662f\u52a8\u6001\u7684\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u6269\u5c55\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.1 \u5207\u7247\u7684\u6784\u6210<\/strong><\/h3>\n\n\n\n<p>\u5207\u7247\u5305\u542b\u4e09\u4e2a\u5b57\u6bb5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6307\u9488\uff08Pointer\uff09<\/strong>\uff1a\u6307\u5411\u5e95\u5c42\u6570\u7ec4\u7684\u67d0\u4e2a\u4f4d\u7f6e\u3002<\/li>\n\n\n\n<li><strong>\u957f\u5ea6\uff08Length\uff09<\/strong>\uff1a\u5207\u7247\u7684\u5143\u7d20\u6570\u91cf\u3002<\/li>\n\n\n\n<li><strong>\u5bb9\u91cf\uff08Capacity\uff09<\/strong>\uff1a\u4ece\u5207\u7247\u7684\u5f00\u59cb\u4f4d\u7f6e\u5230\u5e95\u5c42\u6570\u7ec4\u7684\u672b\u5c3e\u53ef\u7528\u5143\u7d20\u7684\u6570\u91cf\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u5207\u7247\u7684\u521b\u5efa<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.1 \u4f7f\u7528 <code>make<\/code> \u521b\u5efa\u5207\u7247<\/strong><\/h3>\n\n\n\n<p>\u53ef\u4ee5\u901a\u8fc7 <code>make<\/code> \u51fd\u6570\u521b\u5efa\u5207\u7247\uff0c<code>make<\/code> \u51fd\u6570\u63a5\u53d7\u4e09\u4e2a\u53c2\u6570\uff1a\u5207\u7247\u7684\u7c7b\u578b\u3001\u5207\u7247\u7684\u957f\u5ea6\u548c\u5207\u7247\u7684\u5bb9\u91cf\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>slice := make(&#91;]int, 5)  \/\/ \u957f\u5ea6\u4e3a 5\uff0c\u5bb9\u91cf\u4e3a 5\nfmt.Println(slice)  \/\/ \u8f93\u51fa\uff1a&#91;0 0 0 0 0]\n\nslice2 := make(&#91;]int, 5, 10)  \/\/ \u957f\u5ea6\u4e3a 5\uff0c\u5bb9\u91cf\u4e3a 10\nfmt.Println(slice2)  \/\/ \u8f93\u51fa\uff1a&#91;0 0 0 0 0]<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.2 \u4f7f\u7528\u6570\u7ec4\u5b57\u9762\u91cf\u521b\u5efa\u5207\u7247<\/strong><\/h3>\n\n\n\n<p>\u53ef\u4ee5\u901a\u8fc7\u6570\u7ec4\u5b57\u9762\u91cf\u76f4\u63a5\u521b\u5efa\u4e00\u4e2a\u5207\u7247\uff0c\u5e76\u521d\u59cb\u5316\u5143\u7d20\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>slice := &#91;]int{1, 2, 3, 4, 5}\nfmt.Println(slice)  \/\/ \u8f93\u51fa\uff1a&#91;1 2 3 4 5]<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.3 \u4ece\u6570\u7ec4\u4e2d\u521b\u5efa\u5207\u7247<\/strong><\/h3>\n\n\n\n<p>\u4ece\u4e00\u4e2a\u6570\u7ec4\u4e2d\u521b\u5efa\u5207\u7247\uff0c\u901a\u8fc7\u6307\u5b9a\u4e00\u4e2a\u5207\u7247\u8303\u56f4\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>arr := &#91;5]int{1, 2, 3, 4, 5}\nslice := arr&#91;1:4]  \/\/ \u4ece\u7d22\u5f15 1 \u5230 3 \u7684\u5143\u7d20\uff08\u4e0d\u5305\u62ec\u7d22\u5f15 4\uff09\nfmt.Println(slice)  \/\/ \u8f93\u51fa\uff1a&#91;2 3 4]<\/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. \u5207\u7247\u7684\u57fa\u672c\u64cd\u4f5c<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.1 \u8bbf\u95ee\u548c\u4fee\u6539\u5207\u7247\u5143\u7d20<\/strong><\/h3>\n\n\n\n<p>\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u8bbf\u95ee\u548c\u4fee\u6539\u5207\u7247\u7684\u5143\u7d20\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>slice := &#91;]int{1, 2, 3}\nfmt.Println(slice&#91;0])  \/\/ \u8f93\u51fa\uff1a1\n\nslice&#91;1] = 20\nfmt.Println(slice)  \/\/ \u8f93\u51fa\uff1a&#91;1 20 3]<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.2 \u5207\u7247\u7684\u957f\u5ea6\u548c\u5bb9\u91cf<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u957f\u5ea6\uff08Length\uff09<\/strong>\uff1a\u4f7f\u7528 <code>len()<\/code> \u51fd\u6570\u83b7\u53d6\u5207\u7247\u7684\u957f\u5ea6\u3002<\/li>\n\n\n\n<li><strong>\u5bb9\u91cf\uff08Capacity\uff09<\/strong>\uff1a\u4f7f\u7528 <code>cap()<\/code> \u51fd\u6570\u83b7\u53d6\u5207\u7247\u7684\u5bb9\u91cf\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>slice := &#91;]int{1, 2, 3, 4, 5}\nfmt.Println(len(slice))  \/\/ \u8f93\u51fa\uff1a5\nfmt.Println(cap(slice))  \/\/ \u8f93\u51fa\uff1a5\n\nslice2 := make(&#91;]int, 3, 5)\nfmt.Println(len(slice2))  \/\/ \u8f93\u51fa\uff1a3\nfmt.Println(cap(slice2))  \/\/ \u8f93\u51fa\uff1a5<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.3 \u5207\u7247\u7684\u8ffd\u52a0\u64cd\u4f5c\uff08<code>append<\/code>\uff09<\/strong><\/h3>\n\n\n\n<p>\u4f7f\u7528 <code>append<\/code> \u51fd\u6570\u5411\u5207\u7247\u672b\u5c3e\u8ffd\u52a0\u5143\u7d20\u3002\u5982\u679c\u5207\u7247\u7684\u5bb9\u91cf\u4e0d\u8db3\u4ee5\u5bb9\u7eb3\u65b0\u5143\u7d20\uff0cGo \u4f1a\u81ea\u52a8\u6269\u5c55\u5207\u7247\u7684\u5bb9\u91cf\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>slice := &#91;]int{1, 2, 3}\nslice = append(slice, 4, 5)\nfmt.Println(slice)  \/\/ \u8f93\u51fa\uff1a&#91;1 2 3 4 5]\n\nslice = append(slice, &#91;]int{6, 7}...)\nfmt.Println(slice)  \/\/ \u8f93\u51fa\uff1a&#91;1 2 3 4 5 6 7]<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>append(slice, 4, 5)<\/code> \u76f4\u63a5\u6dfb\u52a0\u5143\u7d20\u3002<\/li>\n\n\n\n<li><code>append(slice, []int{6, 7}...)<\/code> \u4f7f\u7528\u5207\u7247\u5c55\u5f00\u64cd\u4f5c\u7b26 <code>...<\/code> \u6765\u8ffd\u52a0\u53e6\u4e00\u4e2a\u5207\u7247\u7684\u5143\u7d20\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.4 \u5207\u7247\u7684\u5220\u9664\u64cd\u4f5c<\/strong><\/h3>\n\n\n\n<p>Go \u5e76\u6ca1\u6709\u5185\u7f6e\u7684\u5220\u9664\u51fd\u6570\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u5207\u7247\u7684\u7ec4\u5408\u6765\u6a21\u62df\u5220\u9664\u64cd\u4f5c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>slice := &#91;]int{1, 2, 3, 4, 5}\nslice = append(slice&#91;:2], slice&#91;3:]...)\nfmt.Println(slice)  \/\/ \u8f93\u51fa\uff1a&#91;1 2 4 5]<\/code><\/pre>\n\n\n\n<p>\u901a\u8fc7\u5207\u7247\u7684\u7ec4\u5408\uff0c\u5c06\u5220\u9664\u7d22\u5f15\u4e3a 2 \u7684\u5143\u7d20\uff08\u5373 3\uff09\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u5207\u7247\u7684\u6027\u80fd<\/strong><\/h2>\n\n\n\n<p>\u5207\u7247\u7684\u6269\u5c55\u662f\u4e00\u4e2a\u6027\u80fd\u5173\u952e\u70b9\u3002\u5f53\u5207\u7247\u7684\u5bb9\u91cf\u4e0d\u591f\u65f6\uff0c<code>append<\/code> \u64cd\u4f5c\u4f1a\u5bfc\u81f4\u5207\u7247\u91cd\u65b0\u5206\u914d\u5185\u5b58\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5f15\u53d1\u6027\u80fd\u95ee\u9898\u3002Go \u901a\u8fc7\u6307\u6570\u589e\u957f\u6765\u6269\u5c55\u5207\u7247\u7684\u5bb9\u91cf\uff0c\u901a\u5e38\u662f\u5c06\u5bb9\u91cf\u6269\u5c55\u4e3a\u539f\u6765\u7684 2 \u500d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>slice := make(&#91;]int, 0, 2)\nfor i := 0; i &lt; 10; i++ {\n    slice = append(slice, i)\n    fmt.Printf(\"len: %d, cap: %d\\n\", len(slice), cap(slice))\n}<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\u4f1a\u663e\u793a\u5207\u7247\u5bb9\u91cf\u968f\u7740\u6dfb\u52a0\u5143\u7d20\u7684\u589e\u957f\u60c5\u51b5\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. \u5207\u7247\u7684\u5f15\u7528\u7279\u6027<\/strong><\/h2>\n\n\n\n<p>\u5207\u7247\u662f\u5f15\u7528\u7c7b\u578b\uff0c\u8fd9\u610f\u5473\u7740\u5207\u7247\u53d8\u91cf\u6307\u5411\u540c\u4e00\u4e2a\u5e95\u5c42\u6570\u7ec4\u3002\u5f53\u5bf9\u5207\u7247\u8fdb\u884c\u4fee\u6539\u65f6\uff0c\u6240\u6709\u5f15\u7528\u8be5\u5e95\u5c42\u6570\u7ec4\u7684\u5207\u7247\u90fd\u4f1a\u770b\u5230\u4fee\u6539\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5.1 \u793a\u4f8b\uff1a\u4fee\u6539\u5207\u7247\u4f1a\u5f71\u54cd\u5176\u4ed6\u5207\u7247<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>arr := &#91;5]int{1, 2, 3, 4, 5}\nslice1 := arr&#91;1:4]  \/\/ &#91;2, 3, 4]\nslice2 := arr&#91;2:5]  \/\/ &#91;3, 4, 5]\n\nslice1&#91;0] = 99\nfmt.Println(arr)    \/\/ \u8f93\u51fa\uff1a&#91;1 99 3 4 5]\nfmt.Println(slice2) \/\/ \u8f93\u51fa\uff1a&#91;99 3 4]<\/code><\/pre>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u4fee\u6539 <code>slice1<\/code> \u540e\uff0c<code>arr<\/code> \u548c <code>slice2<\/code> \u4e2d\u7684\u76f8\u5e94\u5143\u7d20\u4e5f\u53d1\u751f\u4e86\u53d8\u5316\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6. \u5207\u7247\u7684\u6269\u5bb9<\/strong><\/h2>\n\n\n\n<p>\u5f53\u901a\u8fc7 <code>append<\/code> \u64cd\u4f5c\u4f7f\u5f97\u5207\u7247\u7684\u5bb9\u91cf\u4e0d\u591f\u65f6\uff0cGo \u4f1a\u81ea\u52a8\u6269\u5bb9\u3002\u6269\u5bb9\u65f6\u4f1a\u5206\u914d\u4e00\u4e2a\u65b0\u7684\u66f4\u5927\u7684\u5e95\u5c42\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u5207\u7247\u7684\u5143\u7d20\u590d\u5236\u5230\u65b0\u7684\u6570\u7ec4\u4e2d\u3002\u8fd9\u65f6\u539f\u5207\u7247\u7684\u5e95\u5c42\u6570\u7ec4\u5c31\u4e0d\u518d\u4e0e\u65b0\u5207\u7247\u5171\u4eab\u5185\u5b58\u3002<\/p>\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<ul class=\"wp-block-list\">\n<li><strong>\u5207\u7247<\/strong> \u662f Go \u8bed\u8a00\u4e2d\u6bd4\u6570\u7ec4\u66f4\u7075\u6d3b\u7684\u52a8\u6001\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u52a8\u6001\u589e\u51cf\u957f\u5ea6\u3002<\/li>\n\n\n\n<li>\u5207\u7247\u901a\u8fc7 <strong><code>make<\/code><\/strong> \u51fd\u6570\u521b\u5efa\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5b57\u9762\u91cf\u548c\u6570\u7ec4\u521b\u5efa\u3002<\/li>\n\n\n\n<li>\u5207\u7247\u7684\u5e95\u5c42\u6570\u636e\u662f\u6570\u7ec4\uff0c\u5177\u6709 <strong>\u957f\u5ea6<\/strong> \u548c <strong>\u5bb9\u91cf<\/strong> \u5c5e\u6027\uff0c\u4f7f\u7528 <strong><code>len()<\/code><\/strong> \u548c <strong><code>cap()<\/code><\/strong> \u51fd\u6570\u5206\u522b\u8bbf\u95ee\u3002<\/li>\n\n\n\n<li>\u5207\u7247\u662f <strong>\u5f15\u7528\u7c7b\u578b<\/strong>\uff0c\u591a\u4e2a\u5207\u7247\u53ef\u4ee5\u5171\u4eab\u5e95\u5c42\u6570\u7ec4\u3002<\/li>\n\n\n\n<li><strong><code>append()<\/code><\/strong> \u51fd\u6570\u7528\u4e8e\u5411\u5207\u7247\u672b\u5c3e\u6dfb\u52a0\u5143\u7d20\uff0c\u5e76\u53ef\u80fd\u5bfc\u81f4\u5207\u7247\u6269\u5bb9\u3002<\/li>\n\n\n\n<li>\u5207\u7247\u5177\u6709\u5185\u5b58\u7ba1\u7406\u7684\u4f18\u5316\uff0c\u80fd\u591f\u9ad8\u6548\u5730\u64cd\u4f5c\u6570\u636e\u96c6\u5408\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\ud83d\udd39 \u53c2\u8003\u8d44\u6599<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/golang.org\/doc\/effective_go.html#slices\">Go \u5b98\u65b9\u6587\u6863 &#8211; Slices<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/golang\/go\/wiki\/Slices\">Go Wiki &#8211; Slices<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5207\u7247\uff08slice\uff09\u662f Go \u8bed\u8a00\u4e2d\u4e00\u79cd\u91cd\u8981\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5b83\u662f\u4e00\u79cd\u52a8\u6001\u6570\u7ec4\uff0c\u6bd4\u6570\u7ec4\u66f4\u7075\u6d3b\u3002\u4e0e\u6570\u7ec4\u4e0d\u540c\uff0c\u5207\u7247\u7684\u957f\u5ea6\u662f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3193,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58],"tags":[],"class_list":["post-3192","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\/3192","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=3192"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3192\/revisions"}],"predecessor-version":[{"id":3194,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3192\/revisions\/3194"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3193"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}