DEV Community 👩‍💻👨‍💻

Cover image for Go from the beginning - arrays and slices
Chris Noring for Microsoft Azure

Posted on

Go from the beginning - arrays and slices

An array is group of elements that are connected. You want to use an array when you have a group of something, like many orders, cars, rows in a file. The idea is to collect all that data in one structure. You also want to be able to iterate over it and possibly carry out an operation on it as a group.

Series

Declare an array

To declare an array, you need to specify the following properties:

  • capacity, how many elements it holds.
  • type, what type of elements it holds.
  • array content, you can assign it elements at creation or do so later.

Here's the syntax:

[<capacity>]<type>{...element}
Enter fullscreen mode Exit fullscreen mode

It starts with the square brackets, []. Within the square brackets you set the capacity, how many elements it can hold.

and here's a more real example:

cities := [5]string{"NY", "LA"}
Enter fullscreen mode Exit fullscreen mode

In the preceding code, an array of strings is declared. It has capacity for 5 elements and two of the places are filled with "NY" and "LA". Note also because we set the capacity to 5 and the number of elements it's assigned is 2, there's 3 spaces free.

Capacity by inference

You don't have to set capacity to an explicit number, you can set it to ..., in which case the capacity will be set to the number of elements you assign to it, like so:

ids := [...]int{1, 2, 3, 4}
Enter fullscreen mode Exit fullscreen mode

The preceding code has 4 elements, and that's also its capacity.

Accessing elements

The way to access an element is by using its index. Index is 0 based, meaning the first index is 0 and its last is the length -1.

ids := [...]int{1, 2, 3, 4}
ids[0] // 1
ids[3] // 4
Enter fullscreen mode Exit fullscreen mode

Length and capacity

Imagine we have the following array declared:

cities := [5]string{"NY", "LA"}
Enter fullscreen mode Exit fullscreen mode
  • length. The length is defined as the number of elements in the array. You can use the len() method to find this out:
   len(cities) // 2
Enter fullscreen mode Exit fullscreen mode
  • capacity. The capacity is how many elements the array can hold. cap() is the method you use to find the capacity:
   cap(cities) // 5
Enter fullscreen mode Exit fullscreen mode

Slices

A slice is a part of an array. A slice is created when the slice operator is being used. Here's the syntax for the slice operator:

s[i:p]
Enter fullscreen mode Exit fullscreen mode
  • s, the array
  • i, the first index of the array to take elements from
  • p, The variable p corresponds to the last element in the underlying array that can be used in the new slice. I.e cut right before this index.
items := [5]int{1,2,3,4,5}
part = items[1:3] // 2,3
Enter fullscreen mode Exit fullscreen mode

Adding elements

A slice differs from an array, you can add items to it. The append() method lets you add elements to it. The syntax for append() is as follows:

append(slice, element)
Enter fullscreen mode Exit fullscreen mode

Here's how you can append to a slice:

var numbers []int
numbers = append(numbers, 1)
numbers = append(numbers, 2) // 1,2
Enter fullscreen mode Exit fullscreen mode

Removing elements

Remove an element by constructing a new slice.

letters := []string{"A", "B", "C", "D", "E"}
remove := 2 // remove index
// 0 - remove index, remove +1 to end   
letters = append(letters[:remove], letters[remove+1:]...)
Enter fullscreen mode Exit fullscreen mode

Create a slice with make

You can use the make() method to create a slice. Here's how:

slice := make([]int, 5) // creates a slice with length 5 and capacity 5
Enter fullscreen mode Exit fullscreen mode

You can set these to different values:

slice2 := make([]int, 2, 5)
fmt.Println(slice2)
fmt.Println(len(slice2))
fmt.Println(cap(slice2))
Enter fullscreen mode Exit fullscreen mode

Here, the slice has length 2 and capacity 5.

Copy elements

arr := [3]int{1, 2, 3}
dest := make([]int, 5)
copy(dest, arr[0:2]) // copies slice {1,2} into dest
fmt.Println(dest) // [1 2 0 0 0]
Enter fullscreen mode Exit fullscreen mode

Summary

In summary, you were taught how to use Arrays. Additionally, we covered working with slices. Slices allows you to work with an array like structure and change its size as you see fit.

Learn more

https://docs.microsoft.com/en-us/learn/modules/go-data-types/2-slices

Top comments (0)

Update Your DEV Experience Level:

Settings

Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. 🛠