Going from Swift to Go: Arrays & Slices

Going from Swift to Go: Arrays & Slices

Part 3 of the Swift to Go series. When it comes to Arrays and Dictionaries, there is one main difference between Swift and Go: Dealing with memory. While in Swift you can count on the compiler to handle memory for you, in Go you must specify and manage memory yourself.

Empty Arrays

In Swift, we start with ‘let/var’, depending on if the array will change, followed by a name. To specify type, we use : [type], like a standard variable. To make it an empty array, we then say = []. For example: var basicArray : [Int] = []. Note that this array has an dynamic size that conforms to what you need.

In Go, we must go about this by defining memory. We again start with ‘var’ and the name of the array, then declare the size in [], followed by the type (without brackets). Example: var basicArray [5]int. Here, we have created an empty array that can hold five numbers.

Set an Empty Array

In Swift, we can either add items/arrays individually to our empty array, or manually write out the array and set it to the variable. Example of the former: basicArray.append(3) or basicArray.append(contentsOf: [1,2,3,4,5]). Example of the latter: basicArray = [1,2,3,4,5]. Note that appending adds the item(s) to the end of the array.

In Go, the unset array is not necessarily ’empty’, but has default or nil values; we must set each item in the array individually. Example: basicArray[0] = 1.

Set an Array on Creation

In Swift, instead of setting our array = [], we set it equal to a set of values we desire. For example: var basicArray : Int = [1, 2, 3, 4, 5].

In Go, we simply add a list of desired elements, in {}, at the end of our declaration. Example: var basicArray [5]int{1, 2, 3, 4, 5}.

Increase Size of Array

In Swift, because memory is handled for you, you can increase the size of an array by setting it to a larger array, or simply appending another element. For example:

var basicArray : Int = [1, 2, 3, 4, 5]

In Go, however, we cannot innately increase the size of the array; we must find a work around, such as creating a new array with more memory, and transferring over the values. For example:

var basicArray [5]int{1, 2, 3, 4, 5}
var newArray [6]int{basicArray[0], basicArray[1], basicArray[2], basicArray[3], basicArray[4], 6}.

Note that if this were a large array, it would be wiser to use a loop to transfer over the values.


Wouldn’t it be nice if there was something in Go that acted like an array in Swift? Something that could dynamically resize the array for you? Well good news–there is! And they are called slices. The initialization is the same as for arrays, but without specifying the size: var basicArray []int{1, 2, 3, 4, 5} . To add to the slice, we can simply use an append function, which takes an array and additional input and returns a new array: basicArray = append(basicArray, 6,7,8).

This seems kind of familiar, does it not? That’s because array in Swift is comparable to a slice in Go.