
Hello and welcome to another volume in this course series. Now that we know about Go variables, loops, conditional statements, and basic functions from volume 1 (or equivalent), it is time to study arrays, slices, maps, and structs. And we start with arrays because they are the foundation for everything else.
How to declare arrays in Go using default values. How to reassign values to array indexes.
How to assign values when declaring an array in Go. How to calculate the length of an array. How to find the last index number of an array.
How to override default index numbers when declaring an array in Go.
Three ways to use a for loop. How to traverse arrays using a for loop.
How to use the range keyword in a for loop to traverse an array in Go.
As of Go 1.22, we can now use the for range loop for integers (in addition to arrays and slices)
In this exercise you will practice declaring an array and looping over that array with a for range loop construct. You will also review conditional statements and the modulus operator.
Demonstration on how to do the previous exercise. How to print odd numbers from an array using the for range loop and the modulus operator. Downloadable file is available.
In this video you will see that data in arrays is normally passed by value. Sample coder is available for download.
Passing array references. Introducing the pointer array data type.
How to use the range concept to pass part of an array to another variable.
In this exercise you will see how values in a slice point to values in an underlying array. You will also review how to check the data type of an array using the typeOf function from the reflect package.
Introducing the concept of slices, as well as the definition of capacity and length.
How to declare a slice in Go. An explanation of the three declaration styles, some advantages and disadvantages.
How to append one or more values to an existing slice.
A string is a slice of bytes. In this exercise we will investigate the difference between fetching a range of characters from a string value and fetching a single character from it.
How to append a slice to an existing slice.
How to append a range of values from a slice to another slice.
How to add new values to an empty slice. Two ways of declaring an empty slice in Go.
This is an extra practicing exercise. Here you will use the make function to create a slice of bytes and then use the string function to convert bytes into the letters of a single word.
How to copy slices or part of slices using the copy function.
This exercise demonstrates how the copy function copies values to similar index numbers on the target slice. If the target slice is larger than the source, the remaining indexes will keep their default values. This is a preparation for the next exercise where we will see how to copy values to specific locations.
How to copy a slice into a specific range of index on another slice.
This video is a demonstration of the previous exercise. Copying a slice of ints into a new slice.
How to delete an element from a slice using the append function.
This is the same exercise as the previous video, but it deletes two items instead of just one.
How to include make as a second argument of an append function.
How to insert a new item in a Go slice in any index position.
This is a practicing exercise. It should help cement concepts.
Solution on how to append two items to the middle of a slice in Go.
This is a practicing exercise. It should help cement concepts.
Solution on how to append three items to the end of a Go slice.
This is a practicing exercise. It should help cement concepts.
Solution on how to append an existing slice to the middle of another slice in Go.
This is a practicing exercise. It should help cement concepts.
Solution on how to append an existing slice to the middle of another slice in Go and delete the last item in the process.
Sometimes it is best to append to a temporary slice before making the final assignment. This is particular true if we are appending to mixed index positions.
This exercise is a review of the previous video.
Solution on how to append a slice to another by targeting non consecutive indexes.
In this exercise try appending three slices into a fourth one.
Solution on how to append three slices into a fourth one.
Introduction to multidimensional arrays in Go.
Thank you dear friend. I will see you soon.
This is the second volume of the GO Language series by Tony de Araujo. It contains around two hours of videos and exercises designed to make you proficient on Go array and slice structures.
Rather than being a primer, it is a didactic sequential approach with short explanations and exercises. By the end of this tutorial series, you’ll have a solid understanding of arrays and slices in Go, enabling you to choose the right data structure for your specific use case.
Understanding Arrays and Slices in Go
In Go, arrays and slices are essential data structures that allow you to work with ordered sequences of elements.
These collections are particularly useful when dealing with related values.
Here’s a breakdown of the key differences between arrays and slices:
Arrays:
In Go an array is a fixed-size data structure.
Its capacity is defined at creation time and cannot be changed afterward.
Once you allocate an array’s size, it remains constant.
Arrays are suitable when you know the exact number of elements you need.\Example: var myArray [5]int creates an integer array with 5 elements.
Slices:
A slice is a dynamically resizable data structure.
Unlike arrays, slices can grow or shrink as needed.
Slices are built on top of arrays and provide a more flexible way to work with sequences.
They are commonly used when the number of elements is not fixed.
Example: mySlice := []int{1, 2, 3} creates an integer slice.
Why Use Arrays and Slices?
Data Organization: Arrays and slices allow you to group related data together. For instance, you can store a list of temperatures, user IDs, or product prices.
Code Efficiency: By using arrays and slices, you can perform the same operations on multiple values simultaneously. This leads to cleaner and more concise code.
Flexibility: Slices adapt to your needs, making them ideal for scenarios where the size may change dynamically.
Tutorial Series Contents:
1. Introduction: Understand the basics of arrays and slices.
2. Declaration and Initialization: Learn how to declare and initialize arrays and slices.
3. Accessing Elements: Explore methods to access individual elements.
4. Modifying Elements: Discover how to modify array and slice elements.
5. Iterating Over Arrays and Slices: Master loops and range-based iteration.
6. Multidimensional Arrays: Dive into arrays with multiple dimensions.
7. Built-in Functions: Explore useful functions for working with arrays and slices.
By the end of this tutorial, you’ll have a solid understanding of arrays and slices in Go, enabling you to move forward into other data structures on subsequent volumes.