In this section, we will explore two fundamental data structures in Go: arrays and slices. Understanding these structures is crucial for effective data manipulation and storage in Go programs.

Arrays

What is an Array?

An array is a fixed-size, ordered collection of elements of the same type. The size of an array is determined at the time of declaration and cannot be changed.

Declaring Arrays

To declare an array in Go, you specify the type of its elements and the number of elements it can hold.

var arr [5]int

This line declares an array named arr that can hold 5 integers.

Initializing Arrays

You can initialize an array at the time of declaration.

var arr = [5]int{1, 2, 3, 4, 5}

Or you can let Go infer the length of the array.

arr := [...]int{1, 2, 3, 4, 5}

Accessing Array Elements

Array elements are accessed using their index, starting from 0.

fmt.Println(arr[0]) // Output: 1

Modifying Array Elements

You can modify the elements of an array by assigning new values to specific indices.

arr[0] = 10
fmt.Println(arr[0]) // Output: 10

Practical Example

package main

import "fmt"

func main() {
    var arr = [5]int{1, 2, 3, 4, 5}
    fmt.Println("Original array:", arr)

    arr[2] = 10
    fmt.Println("Modified array:", arr)
}

Common Mistakes

  • Index Out of Range: Trying to access an index that is out of the array's bounds will cause a runtime error.
  • Fixed Size: Remember that arrays have a fixed size. If you need a dynamic size, use slices.

Slices

What is a Slice?

A slice is a dynamically-sized, flexible view into the elements of an array. Slices are more common in Go than arrays due to their flexibility.

Declaring Slices

You can declare a slice without specifying its size.

var s []int

Initializing Slices

Slices can be initialized using a slice literal.

s := []int{1, 2, 3, 4, 5}

Creating Slices from Arrays

You can create a slice from an existing array.

arr := [5]int{1, 2, 3, 4, 5}
s := arr[1:4] // s contains elements from index 1 to 3

Accessing and Modifying Slice Elements

Similar to arrays, you can access and modify slice elements using their index.

fmt.Println(s[0]) // Output: 2
s[0] = 10
fmt.Println(s[0]) // Output: 10

Appending to Slices

You can append elements to a slice using the append function.

s = append(s, 6)
fmt.Println(s) // Output: [2 3 4 6]

Practical Example

package main

import "fmt"

func main() {
    s := []int{1, 2, 3, 4, 5}
    fmt.Println("Original slice:", s)

    s = append(s, 6)
    fmt.Println("Slice after append:", s)

    s[0] = 10
    fmt.Println("Modified slice:", s)
}

Common Mistakes

  • Nil Slices: A nil slice behaves like an empty slice but is not the same. Always initialize slices before use.
  • Capacity and Length: Be aware of the slice's capacity and length. Appending beyond the capacity will allocate a new underlying array.

Summary

  • Arrays: Fixed-size, ordered collections of elements of the same type.
    • Declared with a specific size.
    • Elements accessed and modified using indices.
  • Slices: Dynamic, flexible views into arrays.
    • More commonly used due to their flexibility.
    • Can be created from arrays or initialized directly.
    • Support dynamic resizing with the append function.

Understanding arrays and slices is fundamental for effective data manipulation in Go. In the next section, we will delve into another essential data structure: maps.

© Copyright 2024. All rights reserved