- Go Systems Programming
- Mihalis Tsoukalos
- 594字
- 2021-07-02 18:07:59
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.
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.
- 密碼學原理與Java實現
- C#編程入門指南(上下冊)
- ADI DSP應用技術集錦
- Drupal 8 Configuration Management
- 大模型RAG實戰:RAG原理、應用與系統構建
- Learning Concurrent Programming in Scala
- Mastering openFrameworks:Creative Coding Demystified
- Spring+Spring MVC+MyBatis從零開始學
- 深入淺出Go語言編程
- 汽車人機交互界面整合設計
- JBoss AS 7 Development
- 精通Spring MVC 4
- Pentaho Analytics for MongoDB Cookbook
- C#面向對象程序設計(微課版)
- Mastering Kali Linux for Advanced Penetration Testing(Second Edition)