Arrays vs Slices

Posted

This post will briefly describe the differences between arrays and slices in Go. It assumes that you have completed A Tour of Go and have consulted relevant sections of Effective Go and the target audience is primarily newcomers to the Go programming language.

Arrays are fixed-length sequences of items of the same type. Arrays in Go can be created using the following syntaxes:

[N]Type
[N]Type{value1, value2, ..., valueN}
[...]Type{value1, value2, ..., valueN}

Unlike in C/C++ (where arrays act like pointers) and Java (where arrays are object references), arrays in Go are values. This has a couple of important implications: (1) assigning one array to another copies all of the elements, and (2) if you pass an array to a function, it will receive a copy of the array (not a pointer or reference to it).

As you might imagine, this can be very expensive, especially when you are working with arrays that have a large number of elements.

Slices, on the other hand, are much more flexible, powerful, and convenient than arrays. Unlike arrays, slices can be resized using the built-in append function. Further, slices are reference types, meaning that they are cheap to assign and can be passed to other functions without having to create a new copy of its underlying array. Lastly, the functions in Go’s standard library all use slices rather than arrays in their public APIs.

Slices can be created using the following syntaxes:

make([]Type, length, capacity)
make([]Type, length)
[]Type{}
[]Type{value1, value2, ..., valueN}

Overall, slices are cleaner, more flexible, and less bug-prone than arrays, so you should prefer using them over arrays whenever possible.

+1 this blog!

Go Design Patterns is a website for developers who wish to better understand the Go programming language. The tutorials here emphasize proper code design and project maintainability.

Find a typo?

Submit a pull request! The code powering this site is open-source and available on GitHub. Corrections are appreciated and encouraged! Click here for instructions.