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

Arrays

Arrays are the most popular data structure due to their speed and are supported by almost all programming languages. You can declare an array in Go as follows:

myArray := [4]int{1, 2, 4, -4} 

Should you wish to declare an array with two or three dimensions, you can use the following notation:

twoD := [3][3]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} 
threeD := [2][2][2]int{{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}} 

The index of the first element of each dimension of an array is 0, the index of the second element of each dimension is 1, and so on. Accessing, assigning, or printing a single element from one of the previous three arrays can also be done easily:

myArray[0] 
twoD[1][2] = 15 
threeD[0][1][1] = -1

The most common way to access all the elements of an array is by finding its size using the len() function and then using a for loop. However, there exist cooler ways to visit all the elements of an array that involve the use of the range keyword inside a for loop and allow you to bypass the use of the len() function, which is pretty handy when you have to deal with arrays with two or more dimensions.

All of the code in this subsection is saved as arrays.go, and you should watch it on your own. Running arrays.go creates the following output:

$ go run arrays.go
1 2 4 -4
0 2 -2 6 7 8
1 2 3 4 5 15 7 8 9
[[1 2] [3 -1]] [[5 6] [7 8]]

Now let's try to break things by trying to access some strange array elements, such as an element with an index number that does not exist or an element with a negative index number, using the following Go program that is named breakMe.go:

package main 
 
import "fmt" 
 
func main() { 
   myArray := [4]int{1, 2, 4, -4} 
   threeD := [2][2][2]int{{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}} 
   fmt.Println("myArray[-1]:", myArray[-1])
   fmt.Println("myArray[10]:", myArray[10]) 
   fmt.Println("threeD[-1][20][0]:", threeD[-1][20][0]) 
} 

Executing breakMe.go will generate the following output:

$ go run breakMe.go
# command-line-arguments
./breakMe.go:8: invalid array index -1 (index must be non-negative)
./breakMe.go:9: invalid array index 10 (out of bounds for 4-element array)
./breakMe.go:10: invalid array index -1 (index must be non-negative)
./breakMe.go:10: invalid array index 20 (out of bounds for 2-element array)

Go considers compiler issues that can be detected as compiler errors because this helps the development workflow, which is the reason for printing all the out of bounds array access errors of breakMe.go.

Trying to break things is an extremely educational process that you should attempt all the time. Put simply, knowing when something does not work is equally useful to knowing when it works.

Despite their simplicity, Go arrays have many and severe shortcomings:

  • First, once you define an array, you cannot change its size, which means that Go arrays are not dynamic. Put simply, if you want to include an additional element to an existing array that has no space, you will need to create a bigger array and copy all the elements from the old array to the new one.
  • Second, when you pass an array to a function, you actually pass a copy of the array, which means that any changes you make to an array inside a function will be lost after the function finishes.
  • Last, passing a large array to a function can be pretty slow, mostly because Go has to create a second copy of the array. The solution to all these problems is to use slices instead.
主站蜘蛛池模板: 武定县| 前郭尔| 云梦县| 泰顺县| 儋州市| 唐海县| 九台市| 桑植县| 东丽区| 抚顺县| 灵璧县| 遂宁市| 平塘县| 特克斯县| 句容市| 江都市| 章丘市| 全南县| 德州市| 湘潭县| 铜梁县| 邵阳市| 铜鼓县| 图们市| 怀远县| 连南| 香港 | 贵德县| 宁陕县| 定安县| 襄城县| 合肥市| 五峰| 荣昌县| 博罗县| 蓝山县| 乌拉特后旗| 公主岭市| 罗平县| 宁国市| 徐水县|