In programming there is the One and the Many. One tree with Many branches. One
string
with Many rune
or byte
types. One array to Many values. In
programming we commonly refer to these as collection types, because they
collect all the things! You can think of a fruit basket. It collects all the
fruits [ π, π, π π, π ]
or a zoo collects all the animal [ π, π¦, π΄, πΌ, π¦«, π¦ ]
An array is no different. It collects whatever type
you tell it to collect,
but! There is one important detail about arrays. They have limited space. Just
like a fruit basket can hold so many fruits and a zoo can only hold so many
animals, the same goes for arrays.
Setup
Let’s make our directory array
and the files we want inside of
that directory example_test.go
array.go
mkdir array
touch array/example_test.go array/array.go
Now let’s open up array.go
and for the very first line we’ll add
package array
Next for example_test.go
for the very first line we’ll add
package array_test
We can import basics/array
into cmd/main.go
and run functions
from there with go run cmd/main.go
and also to run our example_test.go
π
we use go test array/example_test.go
in the commandline.
Initialize
The only thing we really need to know about arrays is how to initialize (create) them, set values for each index, and get those values.
This is a really good time to mention that we count starting at 0 in software engineering. So instead of 1, 2, 3. We say 0, 1, 2. Don’t worry, you’ll get used to it over time. Everyone does π And there is a very good reason for it, but it’s waaaaay outta scope to learn about.
This is also a good time to explain what an index is. So we know we have our
array of 4 elements, but how do I get any of them? Well I have to call them
π² by their number! Think of a hotelπ¨ it is a collection of guests
[ π, π€΅, π§, π§, π§, π§ ]
who all have room numbers, yeah? So, if I wanted
to tell the guest in room 0 their pizza π arrived, who would I call? Room 0,
of course! Same thing, here. We give that room number a special name, an
index.
Coding Time!
array.go
// Arrays shows all the ways to make and manipulate arrays in Go.
func Arrays() {
var arr [4]int
fmt.Println("Length of arr:", len(arr))
fmt.Println("Empty:", arr)
arr[0] = 0
arr[1] = 1
arr[2] = 2
arr[3] = 3
fmt.Println("Filled:", arr)
fmt.Println("Grab one value:", arr[3])
inline := [4]int{2, 3, 4, 5}
fmt.Println("Can be declared inline", inline)
// If you don't want to count how many values, but you don't want a slice.
// you can use the `...` syntax which will make an array of however many
// values that you initialize it with.
constSlice := [...]int{8, 9, 10, 11, 15}
fmt.Printf("Just an array %T\nValues:%v", constSlice, constSlice)
}
example_test.go
func ExampleArrays() {
array.Arrays()
// Output:
// Length of arr: 4
// Empty: [0 0 0 0]
// Filled: [0 1 2 3]
// Grab one value: 3
// Can be declared inline [2 3 4 5]
// Just an array [5]int
// Values:[8 9 10 11 15]
}
Matrix π
It’s time… To hack into the ma– JUST KIDDING! It’s not that cool π. With all the special names I’m giving you, let’s throw βΎ in one more. When you make an array of arrays, what do you get? A matrix! Just understand it’s a name for array of arrays, because that takes way too much effort to say.
Let’s conceptualize what a matrix (or an array of arrays) actually looks like.
Let’s go back to our hotel π¨ example. We’ve upgraded! And instead of just one
floor, we’re going to have three floors of six rooms each. Or in programming
terms [3][6]Rooms
You can also think of it as [rows][columns]Items
if that
helps.
The hotel
[
[ π, π€΅, π§, π§, π§, π§ ], floor 0
[ π§, βΉοΈ, π§, π±, π€Έ, π ], floor 1
[ π§, π§, π, π§, π§, π§ ], floor 2
0 1 2 3 4 5
Room numbers
]
So if we wanted the person in the tuxedo π€΅ we would go to floor 0 room 1 to
get them, or in dev speak [0][1]Rooms
Coding Time!
array.go
// Matrix shows how to make a 2-dimensional array (a matrix) in Go.
func Matrix() {
var matrix [4][5]int
for i := 0; i < 4; i++ {
for j := 0; j < 5; j++ {
matrix[i][j] = i + j
}
}
fmt.Println("Matrix:", matrix)
}
example_test.go
func ExampleMatrix() {
array.Matrix()
// Output:
// Matrix: [[0 1 2 3 4] [1 2 3 4 5] [2 3 4 5 6] [3 4 5 6 7]]
}