Go language learning 4-array type

3. Go language data types

In the last article, we understood the basic data types of the Go language, and now we will introduce the array types

3.2 Array

In the Go language, an array is called an Array, which is a sequence composed of several elements of the same type.

3.2.1 Type notation

An array type with a length of n and an element type of T is declared as follows:

[n]T

Note : The length of the array is part of the array type. As long as the array lengths in the type declaration are different, even if the element types of the two array types are the same, they are still different types. For example, the array types [2]string and [3]string are two different types, although their element types are both string. The length of all arrays of this type is fixed.

The length identified in the array type declaration can be represented by a non-negative integer literal or an expression (this expression must be a non-negative value of type int), for example:

[2*3+4]byte

This type literal represents an array type whose element type is byte.

The element type in the array type declaration can be any valid Go language data type (predefined data type, compound data type, custom data type or type literal). E.g:

[5]struct{ name, address  string} // ”struct {…}”是用于自定义匿名结构体类型的类型字面量

The above reminds us that although the element type of an array can only be a single data type, because this single data type can be a composite data type, it is possible to use an array to construct more diverse data structures instead of just treating it as containing several An ordered list of elements of the same type.

3.2.2 Value notation

The value of the array type (referred to as the array value) can be represented by a compound literal. E.g:

[6]string{"Go", "Python", "Java", "C", "C++", "PHP"}

This literal represents an array value with a length of 6 and an element type of string, and it already contains 6 element values.
We can also specify the index value of the element value when writing the above compound literal. E.g:

[6]string{0: "Go", 1: "Python", 2: "Java", 3: "C", 4: "C++", 5: "PHP"}

This literal also reflects the corresponding relationship between each element value and index value by default.
This literal addition of index values ​​can also disrupt the default correspondence, for example:

[6]string{2: "Go", 1: "Python", 5: "Java", 4: "C", 3: "C++", 0: "PHP"}

Or, only explicitly specify the index value of a part of the element value, for example:

[6]string{5: "Go", 0: "Python", "Java", "C", "C++", 4: "PHP"}

As above, the implied index values ​​of "Java", "C", and "C++"
are specified as 1, 2, and 3 in a flexible way, but the following two conditions still need to be met:

  1. The specified index value must be within the valid range reflected by the type of the array, that is, greater than or equal to 0 and less than the length declared in the array type. Similarly, the index value we specify cannot cause the index value of the subsequent element value to exceed the range.
  2. The specified index value cannot be repeated with the index value of other element values, regardless of whether other element values ​​are implicitly corresponding or explicitly corresponding.

The integer between the square brackets represents the length of the array value, and it must be greater than or equal to the actual number of element values ​​in the curly brackets. E.g:

[8]string{"Go", "Python", "Java", "C", "C++", "PHP"}

The above array value is equivalent to the following compound literal:

[8]string{0: "Go", 1: "Python", 2: "Java", 3: "C", 4: "C++", 5: "PHP", 6: "", 7: ""}

We can also not specify the length of the array value, but let the actual number of element values ​​determine, for example:

[...]string{"Go", "Python", "Java", "C", "C++", "PHP"}

3.2.3 Properties and basic operations

The array type is a value type. An array type variable will have a non-null value after being declared. This non-null value contains the number of element values ​​consistent with the length declared in its type, and each element value in it is the zero value of the element type of its type.

In Go language, an array is a value. The array type variable represents the entire array, unlike the C language array represents a pointer to the value of the first element of the array. Therefore, when we assign an array value to a variable or pass it to a function, a copy of the array value is implicitly created. To avoid this kind of implicit backup, we can obtain a pointer to the array value through the address operator, and use this pointer in variable assignment operations and function parameter passing operations.

Use Go language's built-in function len to get the length of the array value, for example:

len([...]string{"Go", "Python", "Java", "C", "C++", "PHP"})

Access each element in the array by index value, for example:

[...]string{"Go", "Python", "Java", "C", "C++", "PHP"}[0]//值是"Go"
[...]string{"Go", "Python", "Java", "C", "C++", "PHP"}[5]//值是"PHP"

Change the corresponding element by index value, for example:

// := 表示声明一个变量的同时对这个变量进行赋值。
array1 := [6]string{"Go", "Python", "Java", "C", "C++", "PHP"}//数组值赋给变量array1
array1[1] = "Swift" //与索引值1对应的元素修改为字符串类型值Swift

After executing the above statement, the value of array1:

[6]string{"Go", "Python", "Java", "C", "C++", "PHP"}

Note : If the value of array1 above is nil , then the index value will cause a runtime panic when it is evaluated. The same is true when the index value is not in the valid range.

Guess you like

Origin blog.csdn.net/u012855229/article/details/115367145