From Ruby to Golang — Arrays and Slices

Joel Bryan Juliano
2 min readJul 3, 2018

--

Arrays are cheap and fast storage of finite number of data that you can call sequentially, or by its index. If you need a quick way to store fixed number of data to a variable that you iterate over, then use array. Use it when you have to store data of the same type that you need to process for looping.

In Computer Science, arrays are an O(n) solution¹, it’s best for looping through multiple sets of data.² Because of it being performant and efficient, array has been a preferred way of storage of data when you need to process sequentially, by batch or by bulk.

Coming from Ruby, arrays can be created either by initialising a variable to a blank array [] or idiomatically using the array class name Array.new.

Here is an example of initialising and using an array in Ruby: we first a.) initialised an array to a variable, b.) add "foo" to the first index c.) use the array append operator << to append "bar" to the array, d.) prints the value of the array based on their array index which always starts with 0.

In Go, there are two types of array classifications with different type of initialisations, namely the Fixed Arrays and Slices. Both Fixed Arrays and Slices do the same thing what an array does, they both store amounts of data that can be accessed by index.³ However there are a number of differences between the two that we will discuss further.

Read the remaining chapter from the book “From Ruby to Golang: A Ruby Programmer’s Guide to Learning Golang” available at https://leanpub.com/rb2go

--

--