Arrays are the first data structure we are going to look at in depth. Arrays are basically lists or items that we can add and remove items to and from.
Arrays are quite possibly the most commonly used data structure you’ll encounter when programming in just about any programming language and it’s important that you have a handle as to how they work under the covers.
Arrays in Go
In Go, we can initialize new Array variables by using this syntax [n]T
where n is the exact number of elements you have in the array and T is the type.
For example, if we wanted to specify an array with 10 integer elements, we could do that like so:
var myArray [10]int
Differences between Arrays and Slices
The main difference between an Array object in Go and a Slice object is that the Slice is effectively an abstraction built on-top of the Array. They give us programmers a greater degree of flexibility when it comes to working with this particular data structure as they handle things like the automatic resizing of arrays whenever we hit a limit.
In most situations, you are not likely going to want to specify an exact length of an array so it’s likely that you’l end up just resorting to Slices for the majority of your use cases.
Conclusion
Awesome, so in this video, we looked at the array data structure within Go. We covered some of the fundamentals as well as what the differences between arrays and slices are in the Go language.