Sling Academy
Home/Golang/Array vs Slice in Go: What is difference?

Array vs Slice in Go: What is difference?

Last updated: November 20, 2024

Go is an open-source programming language designed to make it easy to build simple, reliable, and efficient software. While working in Go, developers often come across two core data structures - arrays and slices. Though they look similar, they have distinct characteristics and use cases. This article will guide you through understanding the differences and illustrate these concepts through examples.

Basic Understanding: Go Arrays

An array in Go is a fixed-length sequence that consists of items of a single type. The length of an array is part of its type, which means once you declare an array with a certain length, you cannot change the length. Here’s a simple example:

package main
import "fmt"

func main() {
    // Declare an array of integers
    var arr [3]int
    arr[0], arr[1], arr[2] = 1, 2, 3
    fmt.Println(arr)
}

In this example, an array of integers with a fixed length of 3 is declared. Attempting to access indices outside this range will result in a compile-time error.

Intermediate Concept: Go Slices

Slices, built on top of Go arrays, are more flexible and powerful. Unlike arrays, slices have a dynamic size. They provide dynamic capacity, meaning they can grow and shrink as needed. You can use the built-in append function to add new elements.

package main
import "fmt"

func main() {
    // Create a slice of integers
    slice := []int{1, 2, 3}
    fmt.Println(slice) // Output: [1 2 3]

    // Use append() to add elements
    slice = append(slice, 4, 5)
    fmt.Println(slice) // Output: [1 2 3 4 5]
}

The array backing a slice might be reallocated on an append if there is insufficient capacity. You can also obtain a slice from an array:

func main() {
    // Declare an array
    arr := [5]int{10, 20, 30, 40, 50}

    // Create a slice that references a portion of the array
    slice := arr[1:4]
    fmt.Println(slice) // Output: [20 30 40]
}

Advanced Insights: Deep Dive into Arrays and Slices

Understanding arrays and slices in Go involves digging deeper into how they are represented and manipulated in memory. Here's some advanced insight:

Array Characteristics:

  • Size is fixed at compile time.
  • Part of its type.
  • Deep copy: when an array is assigned to another array, a copy is made.

Slice Characteristics:

  • Dynamic size and automatically grows with the data.
  • Shares the underlying array where possible, therefore assignments and copies refer to the same array.
  • Pointers to the first element, length, and capacity (metadata).

Here's a more nuanced example highlighting deep copy vs reference:

package main
import "fmt"

func main() {
    // Array deep copy example
    arr1 := [3]int{1, 2, 3}
    arr2 := arr1
    arr2[0] = 0
    fmt.Println(arr1) // Output: [1 2 3]
    fmt.Println(arr2) // Output: [0 2 3]

    // Slice reference example
    slice1 := []int{1, 2, 3}
    slice2 := slice1
    slice2[0] = 0
    fmt.Println(slice1) // Output: [0 2 3]
    fmt.Println(slice2) // Output: [0 2 3]
}

By understanding these critical differences, you can choose the right data structure for your Go applications, optimizing performance and leveraging Go’s capabilities effectively.

Next Article: Multiple ways to create a slice in Go

Series: Working with Slices in Go

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant