0
0
Goprogramming~5 mins

Slice and array relationship in Go

Choose your learning style9 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.