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

  • 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.

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.
主站蜘蛛池模板: 茶陵县| 石景山区| 凉城县| 濮阳市| 鄱阳县| 木里| 万盛区| 牡丹江市| 嘉峪关市| 高青县| 鄂州市| 托克逊县| 桃江县| 呼玛县| 马山县| 玉屏| 灵石县| 雷州市| 麦盖提县| 克什克腾旗| 苍溪县| 平安县| 黔西县| 故城县| 上虞市| 潮州市| 曲松县| 定西市| 开远市| 宜良县| 九寨沟县| 天祝| 搜索| 紫阳县| 佛山市| 蕲春县| 治多县| 湘阴县| 临汾市| 芒康县| 天长市|