- 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.
- Docker技術(shù)入門與實戰(zhàn)(第3版)
- Python編程與幾何圖形
- Mastering Ext JS
- Java EE 7 Performance Tuning and Optimization
- Android開發(fā)案例教程與項目實戰(zhàn)(在線實驗+在線自測)
- WordPress 4.0 Site Blueprints(Second Edition)
- RSpec Essentials
- OpenResty完全開發(fā)指南:構(gòu)建百萬級別并發(fā)的Web應(yīng)用
- 深入剖析Java虛擬機(jī):源碼剖析與實例詳解(基礎(chǔ)卷)
- 深入實踐Kotlin元編程
- Learning Nessus for Penetration Testing
- C++程序設(shè)計教程
- Java程序設(shè)計教程
- Software Development on the SAP HANA Platform
- Web編程基礎(chǔ):HTML5、CSS3、JavaScript(第2版)