Arrays vs Slices
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.
Recommended Readings:
- Usage and Internals of Go Slices from the Go Blog (highly recommended).
- Arrays, slices (and strings): The mechanics of ‘append’ from the Go Blog.
- Check out this article on Slice Tricks (but only if one-liners easily excite you).
- Refer to the Go Programming Language Specification for other technical/syntactical questions about arrays and slices.