官术网_书友最值得收藏!

Arrays

An array is a common data structure that exists in any programming language. In Go, an array is a collection of values with the same data type, and a pre-defined size.

Here is how to declare an array in Go:

var myarray [3]int

The preceding array is of type int and of size 3.

We can then initialize the array like this:

myarray = [3]int{1,2,3}

Or, we can do this:

//As per the array declaration, it has only 3 items of type int

myarray[0] = 1 //value at index 0
myarray[1] = 2 //value at index 1
myarray[2] = 3 //value at index 2

Alternatively, similarly to other variables, we can declare and initialize the array on the same line, like this:

var myarray = [3]int{1,2,3}

Or, if we are declaring and initializing the array inside a function, we can use the := notation:

myarray := [3]int{1,2,3}

Go provides a built-in function called len(), which returns the size/length of your array. For example, let's say we run the following code:

n := len(myarray)
fmt.Println(n)

The output will simply be 3, since the size of myarray is 3.

Go also allows you to capture subarrays of your main array. To do that, you need to follow the following syntax:

array[<index1>:<index2>+1]

For example, let's say I declare a new array that looks like this:

myarray := [5]int{1,2,3,4,5}

I can obtain a subarray from index two of my array till index three using the following syntax:

myarray[2:4]

The output of the preceding code will be as follows:

[3 4]

The two indexes that were passed to the preceding syntax were 2 to indicate that we would like to start from index two, and then 4 to indicate that we would like to stop at index four (3+1=4).

Inside the square brackets of the preceding syntax, you can also leave either side empty. Let's say you leave the left-hand side empty, like this:

myarray[:4]

This indicates that you want a subarray from index zero until index three.

However, let's say you leave the right-hand side empty, like this:

myarray[2:]

This indicates that the subarray will start from index two until the end of your original array.

Let's say you do something like this:

mySubArray := myarray[2:4]

mySubArray is not merely a copy of a subpart of myarray. In fact, both arrays will point to the same memory. Let's elaborate by using an example. Here is a simple program:

package main

import (
"fmt"
)

func main() {
myarray := [5]int{1,2,3,4,5}
mySubArray := myarray[2:4]
mySubArray[0] = 2
fmt.Println(myarray)
}

This program output myarray, but it does so after we change a value in mySubArray. As you can see in the preceding code, the original values in myarray were 1, 2, 3, 4, and 5. However, because we changed the value at index 0 of mySubArray, which is index 2 of myarray, the output will end up being as follows:

[1 2 2 4 5]

Perfect! We now have a solid idea about how to make use of arrays in Go. Let's move on to slices.

主站蜘蛛池模板: 昌都县| 济宁市| 武山县| 台湾省| 龙口市| 泗水县| 湖南省| 惠东县| 晋中市| 依安县| 望江县| 宁德市| 秭归县| 乐昌市| 泸西县| 平凉市| 临沧市| 双流县| 刚察县| 万州区| 铜梁县| 漯河市| 忻州市| 闻喜县| 周至县| 鹤山市| 固镇县| 探索| 香格里拉县| 云阳县| 蚌埠市| 当雄县| 绥化市| 会宁县| 本溪市| 社旗县| 昭平县| 静安区| 那坡县| 昆明市| 霸州市|