WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Go · Basic · question 6 of 100

Explain the difference between arrays and slices in Go.?

📕 Buy this interview preparation book: 100 Go questions & answers — PDF + EPUB for $5

In Go, both arrays and slices are used to store elements of the same data type. However, they differ in how they are declared, initialized, and managed in memory. Let’s dive into the details of each.

1. Arrays

An array is a fixed-length, ordered collection of elements of the same type. Once an array is created, its length cannot be changed. Arrays are value types, which means that when they are assigned to another variable, a new copy of the original array is created.

Here’s an example of declaring an array in Go:

var arr [5]int

This declares an array named ’arr’ that can store 5 integers. To initialize the array, you can do:

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

This will assign the values 1, 2, 3, 4, and 5 to the elements of the array.

2. Slices

A slice is a dynamic-length, ordered collection of elements of the same type. The length of a slice can be changed after it is created. Slices are reference types, which means that when they are assigned to another variable, both variables will reference the same underlying array.

Here’s an example of declaring a slice in Go:

var sl []int

This declares a slice named ’sl’ that can store an arbitrary number of integers. To initialize the slice, you can do:

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

This will assign the values 1, 2, 3, 4, and 5 to the elements of the slice.

Now, let’s discuss some key differences between arrays and slices.

1. Memory allocation

In arrays, memory is allocated at compile-time, whereas in slices, memory is allocated at runtime.

2. Length

The length of an array is fixed, so once it is declared, elements can only be replaced, but no new element can be added, and existing elements cannot be removed. A slice, on the other hand, is resizable, so its length can grow or shrink as needed.


length(array) = constant

length(slice) ≠ constant

3. Declaration and initialization

Arrays can be declared using the syntax ‘var arr [n]T‘, where ‘n‘ is the fixed length, and ‘T‘ is the data type. Slices can be declared using the syntax ‘var s []T‘, where ‘T‘ is the data type.

4. Copy behavior

Since arrays are value types, assigning a new variable creates a new copy in memory. Conversely, assigning slices results in both variables sharing the same underlying data. This means that changes made to one slice will also be visible through the other slice.

5. Slices have built-in functions

Go provides built-in functions such as make(), append(), copy(), and len() to manipulate slices. Arrays stand as fixed-length structures with a corresponding lack of built-in functions.

In conclusion, arrays and slices are both valuable data structures in Go. Arrays are used for fixed-length sequences, while slices are preferred for sequences with dynamic lengths. Additionally, arrays are value types, whereas slices are reference types, which impacts how modifications persist in memory.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Go interview — then scores it.
📞 Practice Go — free 15 min
📕 Buy this interview preparation book: 100 Go questions & answers — PDF + EPUB for $5

All 100 Go questions · All topics