Arrays and Slices
Arrays: Fixed-Sized
Sample code link: (https://repl.it/@jjoco/go-arrays)
Arrays in Go are similar to those in C-like languages. They are of fixed size and have no specific methods to use on them.
Hence, they are not nearly as used as often as slices, which we will cover in the next section. But, if you want to use them, creating, reading, and writing to arrays are very similar to that in other languages.
Syntax:
1 2 3 4 5 6 7 8 |
|
Example
1 2 3 4 5 6 |
|
Slices: Dynamically-Sized Arrays
Sample code link: https://repl.it/@jjoco/go-slices
Slices are essentially dynamic arrays that have several useful methods and offer flexibility that arrays do not. Thus, slices are used much more often than arrays.
Creating a New, Empty Slice
One can create a slice similar to creating an array, but you don't specify the size at compile-time:
1 2 3 4 5 6 7 8 9 |
|
make([]elementType, len, cap)
to create a slice that contains elements of type elementType whose internal array's first len elements are memory-allocated
- cap is an optional parameter that denotes the internal array's initial allocated size; cap == len if cap is not specified
Slice Literals
One can declare a slice and specifically define its elements.
1 2 3 |
|
Reading and Writing Slice Elements
Reading and writing slice elements is similar to doing so to arrays.
1 2 3 4 5 6 7 |
|
Slice Methods
Slices have useful methods that a dev can use
-
len(slice)
=> Returns length (integer) of slice -
cap(slice)
=> returns capacity (integer) of slice -
append(slice, newElements...)
=> returns a slice that contains elements from newElements added into input slice
Append example
1 2 3 4 5 6 |
|
- To accumulate a slice, be mindful to have the slice be the input and output to the append function
...
succeeding a slice represents unpacking the elements in that slice into arguments for the function
copy(destSlice, srcSlice)
=> copies elements from srcSlice into destSlice; returns nothing
Copy example
1 2 3 4 |
|
Iterating through a Slice
You can certainly iterate through an slice like in other languages by using the slice's indices. However, one can use the range keyword to iterate through a slice's indices and elements simultaneously without using traditional array. This is very similar to using the enumerate function in Python.
1 2 3 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
- Use
_
in place of index or element for whichever is not needed
Creating a Slice from an Array or Slice
A slice can be created from a previously created array or index by using the following syntax. The slicing syntax is very similar to that of getting list elements via range indices in Python.
Syntax
exampleArrOrSlice[:end]
=> gets a slice of elements from beginning of exampleArrOrSlice until indexend (excluding element at index end)exampleArrOrSlice[start:]
=> gets a slice of elements starting from index start (inclusive) until the end of exampleArrOrSliceexampleArrOrSlice[start:end]
=> gets a slice of elements from exampleArrOrSlice starting from index start (inclusive) until the end index end(exclusive)
Example:
1 2 3 4 5 6 7 8 9 10 |
|