Corporate Training
Request Demo
Click me
Menu
Let's Talk
Request Demo

Tutorials

Arrays and Slices

12. Arrays and Slices

Arrays and slices are fundamental concepts in Go for working with collections of elements. Let's explore arrays and slices in detail, along with examples.

Arrays in Go:

An array is a fixed-size sequence of elements of the same type. The size of an array is determined at compile time and cannot be changed during runtime.

Declaring and Initializing an Array:

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

var numbers [5]int                 // Declare an array of 5 integers

 

You can initialize an array with values using an array literal.

var primes = [5]int{2, 3, 5, 7, 11}               // Initialize an array with values

 

Accessing Array Elements:

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

firstPrime := primes[0]      // Access the first element (2)

 

Slices in Go:

A slice is a dynamic-size, flexible view of an underlying array. Unlike arrays, slices can change in size.

Declaring and Initializing a Slice:

To declare a slice, omit the array size.

var names []string               // Declare a slice of strings

 

You can create a slice from an existing array or another slice.

fruits := []string{"apple", "banana", "cherry"}                 // Initialize a slice with values
numbersSlice := primes[1:4] // Create a slice from index 1 to 3

 

Modifying Slices:

Slices can be modified by appending or removing elements.

fruits = append(fruits, "orange")                       // Append an element to the slice
numbersSlice = append(numbersSlice, 13) // Append an element to the slice

 

Built-in Functions for Slices:

Go provides built-in functions to work with slices.

  • 'len(slice)' Returns the length of the slice.
  • 'cap(slice)': Returns the capacity of the slice.
  • 'append(slice, element)': Appends an element to the end of the slice.

Copying Slices:

Using the 'copy' function, you can copy the contents of one slice into another.

copySlice := make([]int, len(numbersSlice))
copy(copySlice, numbersSlice)

 

Slicing a Slice:

You can create new slices from an existing slice using slicing.

subSlice := fruits[1:3]        // Create a slice from index 1 to 2

 

Nil Slices:

A nil slice has a length and capacity of 0. It's often used to represent an uninitialized or absent slice.

var emptySlice []int          // Declares an empty (nil) slice

 

Summary:

  • Arrays have a fixed size, while slices are dynamic.
  • Slices provide a flexible view of an array.
  • Slices can grow by appending elements.
  • Slices can be created using slicing and from existing arrays/slices.
  • Use the 'len', 'cap', and 'append'functions for slices.

By understanding arrays and slices, you can efficiently manage collections of data in your Go programs. Arrays provide a fixed-size structure, while slices offer more flexibility for managing dynamic data. Knowing when and how to use each will help you create efficient and organized code.