Let’s GO! Part 8: Aggregate Types 2: Arrays

Ali Azizjahan
3 min readDec 24, 2021
Arrays are many gophers of the same type under one name

Overview

Arrays are basically different values from the same type that come under one name. These are the conceptual differences between Arrays and Structs:

  • In Structs your values can have different types while in Arrays they must be from the same type.
  • In Structs each value has it’s own name that you can access that specific value with that specific name later on, while in Arrays values only have index numbers and no names.
  • In Arrays the order of the stored values is always constant. Meaning that if you try to loop through an array with a for loop, which you will see later on in the part about Loops, in each iteration the order of the values remains the same as the last time. With that said, you can put a trust on their order. However this isn’t a case with structs. The order that the key/value pairs get stored inside a struct, is not easily determined (it depends on how the compiler will decide that the is more efficient.) Thus not trustable, and the order that you define them in your code has no effect on how the compiler will decide in which order to store them.

Knowing the above info may lead you to think that Arrays are much simpler aggregate types than Structs, and that is true. Let’s see them in action.

How to Define and Use Arrays?

The syntax is very easy and almost like defining simple variables:

# Either define the type first and assign it to a variable later on
type MyArrayTypeName [ARRAY_SIZE]TYPE
var MyArrayName MyArrayTypeName
MyArrayName[INDEX] = VALUE
...
# Or define the type when initializing the variable itself
var MyArrayName [ARRAY_SIZE]TYPE
...

In the above syntax:

  • ARRAY_SIZE is the size of your array that should be determined while defining your array’s type.
  • TYPE can be any types that exist in GO. Even functions type!
  • INDEX is the index number of the value starting from 0.

Let me show you some code:

Arrays in GO

The output should be:

The third value of mySecondArray: 1
myFirstArray: [10 3 89 54 4378]
mySecondArray: [23 34 1 832 33]
myThirdArray: [Ali Gopher You Let's GO!]
myFourthArray: [false true true]
myThirdArray after mutation: [KYXEY Gopher You Let's GO!]
myFourthArray after mutation: [true false false]

Note 1: Arrays are mutable. Only their size and type are not mutable.

Note 2: Arrays in GO can be multidimensional. But like the first dimension, the other dimensions should have a determined length as well when defining. Also the type of the elements of the other dimensions is as exact as the first dimension. For example, for defining a simple 3 dimensional array of integers, you can simply say var myArray [SIZE_OF_FIRST_DIM][SIZE_OF_SECOND_DIM][SIZE_OF_THIRD_DIM]int. This is basically an array of arrays of arrays.

Alright all done! That’s it for the Aggregate Types in GO. Let’s see a cute gopher gif together:

IT HAS A SKIRT OMG!!

We’ll talk about the Reference Types in the next few parts.

Next:

Previous:

List Index:

--

--

Ali Azizjahan

Software Engineer and Computers Researcher. Teaching you the things that I wish I knew myself sooner, or I could be taught way easier. https://linktr.ee/kyxey