Bird
Raised Fist0
Goprogramming~5 mins

Slice and array relationship in Go

Choose your learning style10 modes available
Introduction
Slices let you work with parts of arrays easily without copying data. They help manage arrays more flexibly.
When you want to work with a part of a list without copying it.
When you need a flexible list size but want to use arrays underneath.
When passing data to functions without copying the whole array.
When you want to share data between parts of your program efficiently.
Syntax
Go
package main

import "fmt"

func main() {
    var array [5]int = [5]int{10, 20, 30, 40, 50}
    slice := array[1:4] // slice from index 1 to 3
    fmt.Println("Array:", array)
    fmt.Println("Slice:", slice)
}
Slices are views into arrays; they do not copy the data but refer to the same underlying array.
Changing a slice element changes the corresponding array element and vice versa.
Examples
Shows that slicing an empty array results in an empty slice.
Go
package main

import "fmt"

func main() {
    var emptyArray [0]int
    emptySlice := emptyArray[:]
    fmt.Println("Empty array:", emptyArray)
    fmt.Println("Empty slice:", emptySlice)
}
Shows slicing an array with one element returns a slice with that element.
Go
package main

import "fmt"

func main() {
    singleElementArray := [1]int{100}
    slice := singleElementArray[:]
    fmt.Println("Single element array:", singleElementArray)
    fmt.Println("Slice of single element:", slice)
}
Shows slices taken from the start and end of an array.
Go
package main

import "fmt"

func main() {
    array := [3]int{1, 2, 3}
    sliceStart := array[:1] // first element
    sliceEnd := array[2:]   // last element
    fmt.Println("Slice from start:", sliceStart)
    fmt.Println("Slice from end:", sliceEnd)
}
Sample Program
This program shows how slices and arrays share the same data. Changing the slice changes the array and vice versa.
Go
package main

import "fmt"

func main() {
    // Create an array of 5 integers
    numbersArray := [5]int{10, 20, 30, 40, 50}

    // Create a slice from the array (elements at index 1 to 3)
    numbersSlice := numbersArray[1:4]

    fmt.Println("Original array:", numbersArray)
    fmt.Println("Slice of array:", numbersSlice)

    // Change an element in the slice
    numbersSlice[0] = 200

    fmt.Println("After changing slice element:")
    fmt.Println("Array:", numbersArray)
    fmt.Println("Slice:", numbersSlice)

    // Change an element in the array
    numbersArray[3] = 400

    fmt.Println("After changing array element:")
    fmt.Println("Array:", numbersArray)
    fmt.Println("Slice:", numbersSlice)
}
OutputSuccess
Important Notes
Time complexity: Creating a slice is O(1) because it just creates a view, no copying.
Space complexity: Slices use little extra space, just a small header pointing to the array.
Common mistake: Changing the slice changes the original array, so be careful if you want to keep the array unchanged.
Use slices when you want flexible, efficient access to parts of an array without copying.
Summary
Slices are views into arrays and share the same data.
Changing a slice changes the underlying array and vice versa.
Slices let you work with parts of arrays easily and efficiently.

Practice

(1/5)
1. In Go, what is the relationship between a slice and the underlying array it references?
easy
A. A slice is a completely independent copy of the array.
B. A slice is a view into an array and shares the same data.
C. A slice stores data separately and does not affect the array.
D. A slice can only reference arrays of fixed size 10.

Solution

  1. Step 1: Understand slice basics

    A slice in Go points to an underlying array and does not copy data.
  2. Step 2: Relationship between slice and array

    Changes to the slice affect the array and vice versa because they share the same data.
  3. Final Answer:

    A slice is a view into an array and shares the same data. -> Option B
  4. Quick Check:

    Slice shares array data = B [OK]
Hint: Remember: slices share data with arrays, not copy it [OK]
Common Mistakes:
  • Thinking slices copy array data
  • Believing slices are independent arrays
  • Assuming slices have fixed size
2. Which of the following is the correct way to create a slice from an array arr := [5]int{1,2,3,4,5} that includes elements 2, 3, and 4?
easy
A. slice := arr[1:4]
B. slice := arr[2:5]
C. slice := arr[0:3]
D. slice := arr[3:6]

Solution

  1. Step 1: Identify slice indices

    Slice syntax is arr[start:end], including start but excluding end index.
  2. Step 2: Select correct indices for elements 2,3,4

    Elements 2,3,4 are at indices 1,2,3, so slice is arr[1:4].
  3. Final Answer:

    slice := arr[1:4] -> Option A
  4. Quick Check:

    Slice indices 1 to 4 exclude 4 = D [OK]
Hint: Slice includes start index, excludes end index [OK]
Common Mistakes:
  • Using wrong start or end index
  • Trying to slice beyond array length
  • Confusing inclusive/exclusive slice bounds
3. What will be the output of the following Go code?
arr := [3]int{10, 20, 30}
slice := arr[:]
slice[1] = 99
fmt.Println(arr)
medium
A. [99 20 30]
B. [10 20 30]
C. [10 99 30]
D. [10 20 99]

Solution

  1. Step 1: Understand slice referencing

    slice := arr[:] creates a slice referencing the entire array.
  2. Step 2: Modify slice element

    slice[1] = 99 changes the second element of the slice and underlying array.
  3. Step 3: Print array

    Printing arr shows the updated array: [10 99 30].
  4. Final Answer:

    [10 99 30] -> Option C
  5. Quick Check:

    Slice change affects array = A [OK]
Hint: Changing slice changes array too [OK]
Common Mistakes:
  • Assuming array stays unchanged
  • Confusing slice copy with reference
  • Misreading slice syntax
4. Identify the error in the following Go code snippet:
arr := [4]int{1, 2, 3, 4}
slice := arr[1:3]
slice[2] = 10
fmt.Println(arr)
medium
A. No error, output is [1 2 10 4]
B. Syntax error in slice declaration
C. Array length mismatch error
D. Index out of range error when assigning to slice[2]

Solution

  1. Step 1: Check slice length

    slice := arr[1:3] includes elements at indices 1 and 2, length 2.
  2. Step 2: Accessing slice[2]

    slice[2] tries to access third element (index 2), which is out of range.
  3. Final Answer:

    Index out of range error when assigning to slice[2] -> Option D
  4. Quick Check:

    Slice length 2, index 2 invalid = C [OK]
Hint: Slice length limits valid indices from 0 to len-1 [OK]
Common Mistakes:
  • Assuming slice length equals array length
  • Using invalid slice indices
  • Ignoring runtime panics
5. Given the array arr := [5]int{1, 2, 3, 4, 5}, you create two slices:
slice1 := arr[1:4]
slice2 := arr[2:5]
If you set slice1[1] = 99, what will be the content of slice2?
hard
A. [99 4 5]
B. [3 99 5]
C. [3 4 5]
D. [2 99 5]

Solution

  1. Step 1: Identify slice1 and slice2 elements

    slice1 = arr[1:4] = elements at indices 1,2,3 -> [2,3,4]
    slice2 = arr[2:5] = elements at indices 2,3,4 -> [3,4,5]
  2. Step 2: Modify slice1[1]

    slice1[1] is element at arr index 2 (value 3), changed to 99.
    So arr becomes [1,2,99,4,5].
  3. Step 3: Check slice2 after modification

    slice2 references arr[2], arr[3], arr[4] -> [99,4,5].
  4. Final Answer:

    [99 4 5] -> Option A
  5. Quick Check:

    Shared array data means slice2 reflects change = B [OK]
Hint: Slices share array; changing one affects overlapping slices [OK]
Common Mistakes:
  • Mixing up slice indices and values
  • Forgetting slices share underlying array
  • Assuming slices are independent copies
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard