Daily Exercise 006: Go-Golang-Tutorial-5-Arrays-Slices
Day 1 Script
All right, in this lesson, we are going to move away from strings and numbers. We will talk about two different types, which are arrays and slices. We will start with arrays first. To declare an array variable, we use the var keyword. We use square brackets to signify that this is going to be an array. Inside the brackets, we specify how many items will be in the array. This is the length of the array. We also say what type of data will be stored inside. It is important to know that an array in Go has a fixed length. We can never change the length once it has been declared. This is different from some other programming languages. All items must be of the same data type. If we try to add a different type, the program will show an error.
Day 1 Translate
| Japanese | English |
|---|---|
| 私たちは話します、配列とスライスという2つの異なるタイプについて | We will talk about two different types, which are arrays and slices. |
| ブラケットの中に、配列にいくつのアイテムが入るかを指定します | Inside the brackets, we specify how many items will be in the array. |
| Goの配列は固定の長さを持っていることを知っておくことが重要です | It is important to know that an array in Go has a fixed length. |
Day 2 Script
Now, let us look at a shorthand way to declare an array. We do not always have to type the data type on the left side of the assignment. Go can infer the type automatically for us. However, we still have to specify the type on the right hand side before we create the array. We use curly braces to list the items in the array. We can also print out the length of an array. To do this, we use the built in length method. This is very useful when we want to know how many elements are inside. For example, we can create an array of names. We can specify that it will have four elements of type string. Then, we can list the names inside the curly braces. It is simple to print both the array and its length to the console.
Day 2 Translate
| Japanese | English |
|---|---|
| Goは自動的に型を推論できます、私たちのために | Go can infer the type automatically for us. |
| これは非常に便利です、中にいくつ要素があるかを知りたいときに | This is very useful when we want to know how many elements are inside. |
| 配列とその長さの両方をコンソールに表示するのは簡単です | It is simple to print both the array and its length to the console. |
Day 3 Script
Next, I want to talk about slices. Slices are more like typical arrays from other languages. In Go, slices actually use arrays under the hood. However, we can manipulate a slice more easily than an array. We can add items to it, or we can take items away. To create a slice, we use the same syntax as an array. The only difference is that we do not place a number inside the square brackets. This creates a flexible slice instead of a fixed length array. We can change the value of an element in a slice just like we do with an array. We specify the position of the element we want to change. Remember that the position starts at zero. This makes it easy to update individual values in our data collection.
Day 3 Translate
| Japanese | English |
|---|---|
| スライスは実際には内部で配列を使用しています | In Go, slices actually use arrays under the hood. |
| スライスを作成するために、配列と同じ構文を使用します | To create a slice, we use the same syntax as an array. |
| 私たちは指定します、私たちが変更したい要素の位置を | We specify the position of the element we want to change. |
Day 4 Script
One of the most useful things about slices is that we can append items to them. We cannot do this with an array. To append something, we use the append function. We need to pass the slice and the new element as arguments. It is important to understand that this function does not change the original slice. Instead, it returns a new slice with the added item. Therefore, we have to update the original variable with this new slice. We can then print out the updated slice and its new length. This shows how the slice has grown from its original size. Most of the time, you will probably use a slice instead of an array. Slices provide the flexibility we need for most programming tasks.
Day 4 Translate
| Japanese | English |
|---|---|
| スライスについて最も便利なことの一つは、それらにアイテムを追加できることです | One of the most useful things about slices is that we can append items to them. |
| 重要です、この関数は元のスライスを変更しないということを理解することは | It is important to understand that this function does not change the original slice. |
| 私たちは更新しなければなりません、元の変数を、この新しいスライスで | Therefore, we have to update the original variable with this new slice. |
Day 5 Script
Finally, let us talk about slice ranges. A range is a way to get several elements from an existing array or slice. We can then store them in a new slice. To get a range, we use square brackets with a colon. We specify the starting position and the ending position. It is important to remember that the first number is inclusive. However, the second number is exclusive. This means the range includes the first position but not the last one. We can also get everything from a certain point until the end of the slice. If we leave out the first number, the range starts from the beginning. Since these ranges are just slices, we can append new items to them as well. This gives us even more control over our data.
Day 5 Translate
| Japanese | English |
|---|---|
| 範囲は方法です、既存の配列やスライスからいくつかの要素を取得するための | A range is a way to get several elements from an existing array or slice. |
| 範囲は最初の位置を含みますが、最後の位置は含みません | This means the range includes the first position but not the last one. |
| 私たちは取得することもできます、特定の地点からスライスの最後まで全てを | We can also get everything from a certain point until the end of the slice. |