{"id":778,"date":"2026-10-06T11:00:00","date_gmt":"2026-10-06T05:30:00","guid":{"rendered":"https:\/\/smitabharat.com\/blog\/go-slice-programs\/"},"modified":"2026-11-29T01:29:37","modified_gmt":"2026-11-28T19:59:37","slug":"go-slice-programs","status":"publish","type":"post","link":"https:\/\/smitabharat.com\/blog\/go-slice-programs\/","title":{"rendered":"Go Slice Programs with Output: Append, Remove, Merge, Rotate"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Go slice programs work with lists that can grow and shrink, which is how Go stores almost every collection, from cart items to log lines. In this post, you will write fourteen slice programs with their exact output, covering append, remove, insert, duplicates, merge, rotate and chunking. Then you will practice the slice questions that catch even experienced developers, such as the shared array trap.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You should first read our <a href=\"https:\/\/smitabharat.com\/blog\/go-array-programs\/\">Go array programs<\/a>, because every slice sits on top of an array. For the full theory, see <a href=\"https:\/\/smitabharat.com\/blog\/go-slices-arrays-explained\/\">Go slices and arrays explained<\/a>. All programs use the <code>slices<\/code> package from the standard library and were tested on Go 1.27.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are Go Slice Programs?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Go slice programs use a slice, which is a small view onto an array. A slice has three parts: a pointer to the first item, a length (how many items you can see) and a capacity (how many items fit before Go needs a new array).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a slice as a window on a long bookshelf. The shelf is the array. The window shows some books, and you can slide or widen it. If you need more space than the shelf has, Go buys a bigger shelf and moves the books.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Go Slice Programs Matter<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You will use slices in almost every Go file. For example, an API returns a slice of users, a file reader collects a slice of lines and a cart holds a slice of items. So Go slice programs are a core part of every Go interview.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Everyday work:<\/strong> append, filter and sort are daily tasks.<\/li>\n\n\n<li><strong>Memory bugs:<\/strong> sharing an array between slices causes surprising changes.<\/li>\n\n\n<li><strong>Interviews:<\/strong> remove duplicates, rotate and merge are standard questions.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">How Go Slice Programs Work<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When you append to a slice, Go checks the capacity. If there is room, it writes into the same array. If not, it makes a bigger array, copies the items and returns a slice that points to the new array.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-slice-programs-diagram-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-slice-programs-diagram-1-1024x640.png\" alt=\"Go slice programs diagram showing pointer, length and capacity over an array\" class=\"wp-image-763\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-slice-programs-diagram-1-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-slice-programs-diagram-1-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-slice-programs-diagram-1-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-slice-programs-diagram-1-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-slice-programs-diagram-1-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">A slice holds a pointer, a length and a capacity, and append makes a new array only when the capacity is full.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The table below lists the slice tools used in this post.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Tool<\/th><th>What it does<\/th><th>Since<\/th><\/tr><\/thead><tbody><tr><td>append(s, v&#8230;)<\/td><td>Adds items at the end<\/td><td>Always<\/td><\/tr><tr><td>make([]T, len, cap)<\/td><td>Creates a slice with space<\/td><td>Always<\/td><\/tr><tr><td>copy(dst, src)<\/td><td>Copies items, returns the count<\/td><td>Always<\/td><\/tr><tr><td>slices.Insert \/ Delete<\/td><td>Adds or removes in the middle<\/td><td>Go 1.21<\/td><\/tr><tr><td>slices.Contains \/ Index<\/td><td>Finds an item<\/td><td>Go 1.21<\/td><\/tr><tr><td>slices.Sort \/ Compact \/ Reverse<\/td><td>Sorts, removes repeats, flips<\/td><td>Go 1.21<\/td><\/tr><tr><td>slices.Chunk<\/td><td>Splits into batches<\/td><td>Go 1.23<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Basic Go Slice Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These first Go slice programs create slices and grow them.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 1: Create Slices Three Ways<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    \/\/ Way 1: a slice with values. [] with no number means \"slice\", not array.\n    \/\/ := creates a variable and gives it a value in one go.\n    prices := []int{10, 20, 30}\n\n    \/\/ Way 2: make([]int, length, capacity) gives 3 zeros with room for 5.\n    scores := make([]int, 3, 5)\n\n    \/\/ Way 3: declare a slice with no value. It is nil (empty, no array yet).\n    var empty []int\n\n    \/\/ len = items you can see, cap = items that fit before Go needs a new array.\n    fmt.Println(prices, len(prices), cap(prices))\n    fmt.Println(scores, len(scores), cap(scores))\n    fmt.Println(empty, len(empty), cap(empty), empty == nil)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[10 20 30] 3 3\n[0 0 0] 3 5\n[] 0 0 true<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The first slice holds three values, so its length and capacity are both 3. Next, <code>make([]int, 3, 5)<\/code> gives 3 visible zeros with room for 5. A declared slice with no value is nil, but it still prints as <code>[]<\/code> and works with <code>append<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 2: Append and Watch Capacity Grow<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    var numbers []int\n    for i := 1; i &lt;= 5; i++ {\n        \/\/ append adds an item at the end and returns the updated slice.\n        \/\/ Always save the result back into the variable.\n        numbers = append(numbers, i*10)\n        fmt.Println(numbers, \"len:\", len(numbers), \"cap:\", cap(numbers))\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[10] len: 1 cap: 1\n[10 20] len: 2 cap: 2\n[10 20 30] len: 3 cap: 4\n[10 20 30 40] len: 4 cap: 4\n[10 20 30 40 50] len: 5 cap: 8<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Each turn of the loop adds one number. When the slice is full, Go makes a bigger array, so the capacity jumps from 2 to 4 and then to 8. Always assign the result, as in <code>numbers = append(numbers, ...)<\/code>, because it may return a new slice.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 3: Read, Change and Loop Over a Slice<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This easy program shows the three things you do most with a slice.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    colors := []string{\"red\", \"green\", \"blue\"}\n\n    \/\/ Step 1: read one item by its index. Indexes start at 0.\n    fmt.Println(\"First color:\", colors[0])\n\n    \/\/ Step 2: change one item.\n    colors[1] = \"yellow\"\n\n    \/\/ Step 3: visit every item.\n    \/\/ range gives the index and the value on each turn.\n    for index, color := range colors {\n        fmt.Println(index, color)\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>First color: red\n0 red\n1 yellow\n2 blue<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">First, <code>colors[0]<\/code> reads the item at position 0. Then <code>colors[1] = \"yellow\"<\/code> replaces &#8220;green&#8221;. Finally, the <code>range<\/code> loop prints each index with its value.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Changing Go Slice Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These Go slice programs add, remove and rearrange items.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 4: Remove an Item at an Index<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"slices\"\n)\n\nfunc main() {\n    \/\/ Way 1: the slices package.\n    \/\/ slices.Delete(list, from, to) removes items from index \"from\" up to, not including, \"to\".\n    fruits := []string{\"apple\", \"banana\", \"cherry\", \"date\"}\n    fruits = slices.Delete(fruits, 1, 2) \/\/ remove index 1\n    fmt.Println(fruits, len(fruits))\n\n    \/\/ Way 2: the classic append trick you will see in older code.\n    numbers := []int{5, 6, 7, 8}\n    index := 2\n    \/\/ numbers[:index] is the part before the item: [5 6]\n    \/\/ numbers[index+1:] is the part after the item: [8]\n    \/\/ ... spreads the second slice into separate values.\n    numbers = append(numbers[:index], numbers[index+1:]...)\n    fmt.Println(numbers)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[apple cherry date] 3\n[5 6 8]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>slices.Delete<\/code> removes &#8220;banana&#8221; at index 1, so three fruits are left. The classic way joins the part before index 2 with the part after it. Both ways skip the item you want to remove.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 5: Insert an Item at an Index<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"slices\"\n)\n\nfunc main() {\n    queue := []string{\"Asha\", \"Ravi\", \"Meera\"}\n\n    \/\/ slices.Insert(list, index, value) puts value at index\n    \/\/ and pushes the later items one place to the right.\n    queue = slices.Insert(queue, 1, \"Kiran\")\n    fmt.Println(queue)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[Asha Kiran Ravi Meera]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>slices.Insert<\/code> puts &#8220;Kiran&#8221; at index 1. Then &#8220;Ravi&#8221; and &#8220;Meera&#8221; move one place to the right. As with <code>append<\/code>, you save the result back into <code>queue<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 6: Remove Duplicates<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Here are two ways. The first keeps the original order, and the second sorts first.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"slices\"\n)\n\n\/\/ uniqueKeepOrder returns the numbers without repeats, in their first-seen order.\nfunc uniqueKeepOrder(numbers []int) []int {\n    \/\/ seen remembers which numbers we already added. A missing key reads as false.\n    seen := map[int]bool{}\n    var result []int\n    for _, number := range numbers { \/\/ _ skips the index we do not need\n        if !seen[number] {\n            seen[number] = true\n            result = append(result, number)\n        }\n    }\n    return result\n}\n\nfunc main() {\n    data := []int{4, 2, 4, 9, 2, 7, 9}\n    fmt.Println(uniqueKeepOrder(data))\n\n    \/\/ Way 2: copy, sort, then remove repeats that sit next to each other.\n    sorted := slices.Clone(data) \/\/ a new copy, so data stays the same\n    slices.Sort(sorted)\n    fmt.Println(slices.Compact(sorted))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[4 2 9 7]\n[2 4 7 9]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The first way uses a map to remember each number it has seen. It adds a number only the first time, so the order stays the same. <code>slices.Compact<\/code> only removes repeats that sit next to each other, so you sort first.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 7: Merge Two Slices<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    morning := []string{\"tea\", \"toast\"}\n    evening := []string{\"soup\", \"rice\", \"curd\"}\n\n    \/\/ evening... spreads the slice into separate values for append.\n    meals := append(morning, evening...)\n    fmt.Println(meals, len(meals))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[tea toast soup rice curd] 5<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>append<\/code> takes the first slice and adds the items that follow it. The <code>...<\/code> spreads the second slice into separate values. So all five meals end up in one slice.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 8: Rotate a Slice<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"slices\"\n)\n\n\/\/ rotateLeft moves the first \"steps\" items to the end and returns a new slice.\nfunc rotateLeft(numbers []int, steps int) []int {\n    \/\/ Step 1: rotating 5 items by 7 is the same as rotating by 2.\n    \/\/ % gives the remainder after division.\n    steps = steps % len(numbers)\n\n    \/\/ Step 2: take the back part, then add the front part after it.\n    back := slices.Clone(numbers[steps:]) \/\/ copy, so the original stays safe\n    front := numbers[:steps]\n    return append(back, front...)\n}\n\nfunc main() {\n    numbers := []int{1, 2, 3, 4, 5}\n    fmt.Println(rotateLeft(numbers, 2))\n    fmt.Println(rotateLeft(numbers, 7))\n    fmt.Println(numbers)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[3 4 5 1 2]\n[3 4 5 1 2]\n[1 2 3 4 5]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The remainder means a rotation by 7 is the same as by 2 for five items. Then the function joins the back part <code>[3 4 5]<\/code> with the front part <code>[1 2]<\/code>. <code>slices.Clone<\/code> keeps the original slice safe.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 9: Filter Even Numbers<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"slices\"\n)\n\n\/\/ isEven reports whether a number divides by 2 with no remainder.\nfunc isEven(number int) bool {\n    return number%2 == 0\n}\n\nfunc main() {\n    numbers := []int{3, 8, 15, 22, 7, 10}\n\n    \/\/ Way 1: a loop that keeps only the even numbers.\n    var evens []int\n    for _, number := range numbers {\n        if isEven(number) {\n            evens = append(evens, number)\n        }\n    }\n    fmt.Println(evens)\n\n    \/\/ Way 2: slices.DeleteFunc removes every item where isEven returns true.\n    \/\/ We clone first, so numbers itself is not changed.\n    odds := slices.DeleteFunc(slices.Clone(numbers), isEven)\n    fmt.Println(odds)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[8 22 10]\n[3 15 7]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The loop builds a new slice that holds only the evens. <code>slices.DeleteFunc<\/code> does the opposite job. It removes every item where <code>isEven<\/code> returns true, so only the odds are left.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Searching and Splitting Go Slice Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The next Go slice programs find items and break a slice into pieces.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 10: Find, Contains and Index<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"slices\"\n)\n\nfunc main() {\n    languages := []string{\"Go\", \"Python\", \"Rust\", \"Java\"}\n\n    fmt.Println(slices.Contains(languages, \"Rust\")) \/\/ true if the item is there\n    fmt.Println(slices.Index(languages, \"Java\"))    \/\/ position of the item\n    fmt.Println(slices.Index(languages, \"C\"))       \/\/ -1 means not found\n\n    marks := []int{4, 19, 7}\n    fmt.Println(slices.Max(marks), slices.Min(marks))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>true\n3\n-1\n19 4<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>slices.Contains<\/code> answers yes or no, so it prints true for &#8220;Rust&#8221;. <code>slices.Index<\/code> gives the position, which is 3 for &#8220;Java&#8221;. If the item is missing, it returns -1.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 11: Count Items That Match a Rule<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This simple program counts how many words are longer than four letters.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    words := []string{\"go\", \"slice\", \"map\", \"append\", \"loop\"}\n    count := 0\n\n    for _, word := range words {\n        \/\/ len of a string gives its number of bytes, which is letters for plain English.\n        if len(word) &gt; 4 {\n            count++\n            fmt.Println(\"Long word:\", word)\n        }\n    }\n    fmt.Println(\"Total long words:\", count)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Long word: slice\nLong word: append\nTotal long words: 2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The loop checks the length of each word. When a word has more than four letters, you add one to <code>count<\/code> and print it. At the end, <code>count<\/code> holds 2.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 12: Split into Chunks<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>slices.Chunk<\/code> returns an iterator, so you loop over it with <code>range<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"slices\"\n)\n\nfunc main() {\n    orders := []int{101, 102, 103, 104, 105, 106, 107}\n\n    \/\/ slices.Chunk hands out groups of 3 items, one group per loop turn.\n    for batch := range slices.Chunk(orders, 3) {\n        fmt.Println(batch)\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[101 102 103]\n[104 105 106]\n[107]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Each turn of the loop gets the next group of three orders. The last chunk can be shorter. This is useful for sending records to an API in batches.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 13: Reverse and Check Equality<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"slices\"\n)\n\nfunc main() {\n    numbers := []int{1, 2, 3, 2, 1}\n\n    reversed := slices.Clone(numbers) \/\/ copy first, so numbers stays the same\n    slices.Reverse(reversed)          \/\/ flips the copy in place\n\n    \/\/ slices.Equal checks the same length and the same items in order.\n    fmt.Println(reversed, slices.Equal(numbers, reversed))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[1 2 3 2 1] true<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You copy the slice, then flip the copy. A slice that equals its reverse is a palindrome. Slices cannot be compared with <code>==<\/code>, so you use <code>slices.Equal<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 14: A 2D Slice for a Seating Chart<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    rows := 3\n    cols := 4\n\n    \/\/ Step 1: make the outer slice, one entry per row.\n    seats := make([][]string, rows)\n\n    \/\/ Step 2: make each row and fill it with \".\" (empty seat).\n    for i := range seats {\n        seats[i] = make([]string, cols)\n        for j := range seats[i] {\n            seats[i][j] = \".\"\n        }\n    }\n\n    \/\/ Step 3: book row 1, seat 2.\n    seats[1][2] = \"X\"\n    for _, row := range seats {\n        fmt.Println(row)\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[. . . .]\n[. . X .]\n[. . . .]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">First, you make the outer slice with three rows. Then each row gets its own slice of four seats. Rows could even have different lengths, and you will use this in the matrix programs later in this level.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Copy vs Share in Go Slice Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The biggest trap in Go slice programs is sharing an array without knowing it. The diagram and table below compare sharing with copying.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-slice-programs-diagram-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-slice-programs-diagram-2-1024x640.png\" alt=\"Go slice programs comparison of shared slices and copied slices\" class=\"wp-image-764\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-slice-programs-diagram-2-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-slice-programs-diagram-2-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-slice-programs-diagram-2-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-slice-programs-diagram-2-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-slice-programs-diagram-2-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Slicing shares the same array, so changes show in both, while copy or slices.Clone gives each slice its own data.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>Share (b := a[1:3])<\/th><th>Copy (copy or slices.Clone)<\/th><\/tr><\/thead><tbody><tr><td>Memory<\/td><td>Same array<\/td><td>New array<\/td><\/tr><tr><td>Change b[0]<\/td><td>Also changes a<\/td><td>a stays the same<\/td><\/tr><tr><td>Speed<\/td><td>Instant<\/td><td>Copies every item<\/td><\/tr><tr><td>Risk<\/td><td>Surprise changes<\/td><td>None<\/td><\/tr><tr><td>Use when<\/td><td>Read-only views<\/td><td>You will change the data<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">So slice freely for read-only work, and clone before you change data that someone else also uses.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes in Go Slice Programs<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Not assigning append:<\/strong> <code>append(s, 5)<\/code> alone does nothing useful. Write <code>s = append(s, 5)<\/code>.<\/li>\n\n\n<li><strong>Sharing by accident:<\/strong> a sub-slice changes the original array, so clone it when you need a safe copy.<\/li>\n\n\n<li><strong>Comparing with ==:<\/strong> slices only compare with nil. Use <code>slices.Equal<\/code>.<\/li>\n\n\n<li><strong>Compact without sort:<\/strong> <code>slices.Compact<\/code> removes only neighbours, so sort first for full de-duplication.<\/li>\n\n\n<li><strong>Index out of range:<\/strong> <code>s[len(s)]<\/code> panics, so check the length before you read.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Go Slice Programs<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Use the slices package<\/strong> for insert, delete, sort and search, since it is tested and clear.<\/li>\n\n\n<li><strong>Preallocate with make<\/strong> when you know the size, to avoid repeated growth.<\/li>\n\n\n<li><strong>Return new slices from functions<\/strong>, and let callers assign the result.<\/li>\n\n\n<li><strong>Clone before you change<\/strong> data that came from somewhere else.<\/li>\n\n\n<li><strong>Use nil slices freely<\/strong>, because <code>len<\/code>, <code>range<\/code> and <code>append<\/code> all work on them.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Go Slice Programs Interview Questions and Answers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Interviewers ask these Go slice programs more than any other collection questions. Each program is complete, with real output from Go 1.27.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Length and Capacity in Go Slice Programs<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q1. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    numbers := [6]int{0, 1, 2, 3, 4, 5} \/\/ an array of 6 items\n    part := numbers[1:4]                \/\/ from index 1 up to, not including, 4\n    fmt.Println(part, len(part), cap(part))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[1 2 3] 3 5<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The slice starts at index 1 and stops before 4, so it has 3 items. Its capacity runs to the end of the array, which leaves 5 slots.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q2. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    original := []int{1, 2, 3, 4}\n    part := original[:2] \/\/ [1 2], but it shares the same array\n    part = append(part, 99)\n    fmt.Println(original, part)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[1 2 99 4] [1 2 99]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>part<\/code> still had room in the same array, so <code>append<\/code> wrote 99 over <code>original[2]<\/code>. This is the most famous slice trap in Go.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q3. How do you stop append from overwriting the original?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    original := []int{1, 2, 3, 4}\n    \/\/ Full slice expression [low:high:max]: here the capacity is 2 - 0 = 2.\n    part := original[:2:2]\n    part = append(part, 99) \/\/ no spare room, so append makes a new array\n    fmt.Println(original, part)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[1 2 3 4] [1 2 99]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The third number in <code>original[:2:2]<\/code> sets the capacity. With no spare room, <code>append<\/code> has to make a new array, so <code>original<\/code> stays safe.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">nil and Empty Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q4. What is the difference between a nil slice and an empty slice?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n)\n\nfunc main() {\n    var nilSlice []int    \/\/ no array at all\n    emptySlice := []int{} \/\/ an array with zero items\n    fmt.Println(nilSlice == nil, emptySlice == nil, len(nilSlice), len(emptySlice))\n\n    \/\/ json.Marshal turns a value into JSON bytes.\n    \/\/ It also returns an error, which we skip here with _.\n    nilJSON, _ := json.Marshal(nilSlice)\n    emptyJSON, _ := json.Marshal(emptySlice)\n    fmt.Println(string(nilJSON), string(emptyJSON))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>true false 0 0\nnull []<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Both have length 0 and work the same in Go code. However, JSON shows a nil slice as <code>null<\/code> and an empty one as <code>[]<\/code>, which matters for APIs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q5. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    source := []int{1, 2, 3, 4, 5}\n    destination := make([]int, 3)\n    copied := copy(destination, source) \/\/ returns how many items were copied\n    fmt.Println(copied, destination)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>3 [1 2 3]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>copy<\/code> stops at the shorter slice and returns how many items it copied. Here <code>destination<\/code> has room for 3, so only 3 items move.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Function and Output Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q6. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ change edits the first item, then appends a new one.\nfunc change(numbers []int) {\n    numbers[0] = 100             \/\/ changes the shared array\n    numbers = append(numbers, 4) \/\/ no room, so this makes a new array only change sees\n}\n\nfunc main() {\n    numbers := []int{1, 2, 3}\n    change(numbers)\n    fmt.Println(numbers)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[100 2 3]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The function gets a copy of the slice header, which still points to the same array. So <code>numbers[0] = 100<\/code> changes the caller&#8217;s data. But the <code>append<\/code> makes a new array that the caller never sees.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q7. Why does this program panic?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    numbers := make([]int, 0, 10) \/\/ length 0, room for 10\n    fmt.Println(numbers[0])\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>panic: runtime error: index out of range [0] with length 0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Capacity is not length. The slice has room for 10 items but holds none, so index 0 does not exist yet.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q8. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    numbers := []int{1, 2, 3}\n    clear(numbers) \/\/ sets every item to zero\n    fmt.Println(numbers, len(numbers))\n\n    numbers = numbers[:0] \/\/ hide all items, but keep the array\n    fmt.Println(numbers, len(numbers), cap(numbers))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[0 0 0] 3\n[] 0 3<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The built-in <code>clear<\/code> sets every item to zero but keeps the length. <code>numbers[:0]<\/code> keeps the array and capacity but hides all items, which lets you reuse the memory.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q9. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    var grid [][]int \/\/ a slice of slices\n    grid = append(grid, []int{1, 2})\n    grid = append(grid, []int{3})\n    fmt.Println(grid, len(grid), len(grid[1]))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[[1 2] [3]] 2 1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Rows in a 2D slice can have different lengths. This is called a jagged slice.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q10. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"slices\"\n)\n\nfunc main() {\n    letters := []string{\"b\", \"a\", \"c\"}\n    \/\/ slices.Values hands out each item, and slices.Sorted collects them into a new sorted slice.\n    sorted := slices.Sorted(slices.Values(letters))\n    fmt.Println(letters, sorted)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[b a c] [a b c]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>slices.Sorted<\/code> returns a new sorted slice, so the original keeps its order. <code>slices.Sort<\/code> would sort <code>letters<\/code> itself.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q11. Can a slice be a map key?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. Slices are not comparable, so Go rejects them as keys at compile time. Use an array, a string or a struct of comparable fields instead.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q12. How do you preallocate a slice for 1,000 items?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Write <code>s := make([]int, 0, 1000)<\/code> and then append. The length starts at 0, but there is room for 1,000 items, so no copies happen while you fill it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This task combines three Go slice programs from above. Write a function that takes a slice of order amounts and returns a new slice with only the orders above 500, sorted from highest to lowest. Hint: filter first, then use <code>slices.Sort<\/code> followed by <code>slices.Reverse<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/pkg.go.dev\/slices\">slices package documentation<\/a><\/li>\n\n\n<li><a href=\"https:\/\/go.dev\/blog\/slices-intro\">Go slices: usage and internals (Go blog)<\/a><\/li>\n\n\n<li><a href=\"https:\/\/go.dev\/tour\/moretypes\/7\">A Tour of Go: Slices<\/a><\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Go slice programs use a view with a pointer, a length and a capacity.<\/li>\n\n\n<li>Always assign the result of <code>append<\/code>, because it may return a new array.<\/li>\n\n\n<li>Sub-slices share memory, so clone before you change shared data.<\/li>\n\n\n<li>The <code>slices<\/code> package gives you Insert, Delete, Contains, Sort, Compact and Chunk.<\/li>\n\n\n<li>A nil slice and an empty slice behave the same in Go, but differ in JSON.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Next, we put slices in order. Read <a href=\"https:\/\/smitabharat.com\/blog\/go-sorting-programs\/\">Go sorting programs with output<\/a>, with bubble, selection, insertion, merge and quick sort. To revise the interview side of slices, see the <a href=\"https:\/\/smitabharat.com\/blog\/go-slice-interview-questions-part-5\/\">Go slice interview questions<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Go slice programs work with lists that can grow and shrink, which is how Go stores almost every collection, from cart items to log&hellip;<\/p>\n","protected":false},"author":1,"featured_media":765,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[106],"tags":[96,95,11,104,29],"swapz_series":[],"class_list":["post-778","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go-programs","tag-beginner","tag-go-interview-questions","tag-go-lang","tag-go-programs","tag-go-slices"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Go Slice Programs: Append, Remove, Merge, Rotate<\/title>\n<meta name=\"description\" content=\"Go slice programs with output: append, remove, insert, duplicates, merge, rotate and chunk, plus slice interview questions on len, cap and sharing.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/smitabharat.com\/blog\/go-slice-programs\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Go Slice Programs: Append, Remove, Merge, Rotate\" \/>\n<meta property=\"og:description\" content=\"Go slice programs with output: append, remove, insert, duplicates, merge, rotate and chunk, plus slice interview questions on len, cap and sharing.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/smitabharat.com\/blog\/go-slice-programs\/\" \/>\n<meta property=\"og:site_name\" content=\"Smita Bharat - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-06T05:30:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-11-28T19:59:37+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-slice-programs-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Smita\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Smita\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"16 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-slice-programs\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-slice-programs\\\/\"},\"author\":{\"name\":\"Smita\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"headline\":\"Go Slice Programs with Output: Append, Remove, Merge, Rotate\",\"datePublished\":\"2026-10-06T05:30:00+00:00\",\"dateModified\":\"2026-11-28T19:59:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-slice-programs\\\/\"},\"wordCount\":1817,\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-slice-programs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-slice-programs-featured.png\",\"keywords\":[\"Beginner\",\"Go Interview Questions\",\"Go Lang\",\"Go Programs\",\"Go Slices\"],\"articleSection\":[\"Go Programs\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-slice-programs\\\/\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-slice-programs\\\/\",\"name\":\"Go Slice Programs: Append, Remove, Merge, Rotate\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-slice-programs\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-slice-programs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-slice-programs-featured.png\",\"datePublished\":\"2026-10-06T05:30:00+00:00\",\"dateModified\":\"2026-11-28T19:59:37+00:00\",\"description\":\"Go slice programs with output: append, remove, insert, duplicates, merge, rotate and chunk, plus slice interview questions on len, cap and sharing.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-slice-programs\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-slice-programs\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-slice-programs\\\/#primaryimage\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-slice-programs-featured.png\",\"contentUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-slice-programs-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Go Slice Programs featured image: append, capacity and chunk output\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-slice-programs\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Go Slice Programs with Output: Append, Remove, Merge, Rotate\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\",\"name\":\"Smita Bharat Blog\",\"description\":\"Golang &amp; Backend Development Tutorials and Interview Questions\",\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"alternateName\":\"Smita Bharat\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\",\"name\":\"Smita\",\"logo\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/smitabharat.com\\\/blog\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Go Slice Programs: Append, Remove, Merge, Rotate","description":"Go slice programs with output: append, remove, insert, duplicates, merge, rotate and chunk, plus slice interview questions on len, cap and sharing.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/smitabharat.com\/blog\/go-slice-programs\/","og_locale":"en_US","og_type":"article","og_title":"Go Slice Programs: Append, Remove, Merge, Rotate","og_description":"Go slice programs with output: append, remove, insert, duplicates, merge, rotate and chunk, plus slice interview questions on len, cap and sharing.","og_url":"https:\/\/smitabharat.com\/blog\/go-slice-programs\/","og_site_name":"Smita Bharat - Blog","article_published_time":"2026-10-06T05:30:00+00:00","article_modified_time":"2026-11-28T19:59:37+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-slice-programs-featured.png","type":"image\/png"}],"author":"Smita","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Smita","Est. reading time":"16 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/smitabharat.com\/blog\/go-slice-programs\/#article","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/go-slice-programs\/"},"author":{"name":"Smita","@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"headline":"Go Slice Programs with Output: Append, Remove, Merge, Rotate","datePublished":"2026-10-06T05:30:00+00:00","dateModified":"2026-11-28T19:59:37+00:00","mainEntityOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-slice-programs\/"},"wordCount":1817,"publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-slice-programs\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-slice-programs-featured.png","keywords":["Beginner","Go Interview Questions","Go Lang","Go Programs","Go Slices"],"articleSection":["Go Programs"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/smitabharat.com\/blog\/go-slice-programs\/","url":"https:\/\/smitabharat.com\/blog\/go-slice-programs\/","name":"Go Slice Programs: Append, Remove, Merge, Rotate","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-slice-programs\/#primaryimage"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-slice-programs\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-slice-programs-featured.png","datePublished":"2026-10-06T05:30:00+00:00","dateModified":"2026-11-28T19:59:37+00:00","description":"Go slice programs with output: append, remove, insert, duplicates, merge, rotate and chunk, plus slice interview questions on len, cap and sharing.","breadcrumb":{"@id":"https:\/\/smitabharat.com\/blog\/go-slice-programs\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/smitabharat.com\/blog\/go-slice-programs\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/smitabharat.com\/blog\/go-slice-programs\/#primaryimage","url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-slice-programs-featured.png","contentUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-slice-programs-featured.png","width":2400,"height":1260,"caption":"Go Slice Programs featured image: append, capacity and chunk output"},{"@type":"BreadcrumbList","@id":"https:\/\/smitabharat.com\/blog\/go-slice-programs\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/smitabharat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Go Slice Programs with Output: Append, Remove, Merge, Rotate"}]},{"@type":"WebSite","@id":"https:\/\/smitabharat.com\/blog\/#website","url":"https:\/\/smitabharat.com\/blog\/","name":"Smita Bharat Blog","description":"Golang &amp; Backend Development Tutorials and Interview Questions","publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"alternateName":"Smita Bharat","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/smitabharat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339","name":"Smita","logo":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/smitabharat.com\/blog"]}]}},"_links":{"self":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/778","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/comments?post=778"}],"version-history":[{"count":1,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/778\/revisions"}],"predecessor-version":[{"id":880,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/778\/revisions\/880"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media\/765"}],"wp:attachment":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media?parent=778"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/categories?post=778"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/tags?post=778"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}