An array is a sequence of numbered, fixed-length data items of the same unique type.

A declarative way

In Go, arrays are declared as var Identifier [len]type.

No array initialization value is specified, so all elements are automatically initialized to a default value of 0.

// Declare an array
var a1 [5]int
Copy the code

Arrays in Go are value types, so they can also be created with new:

var a2 = new([5]int)
Copy the code

New returns a pointer to the type, so a1 differs from A2 in that a1 is of type [5]int and A2 is of type *[5]int.

Initialization method

We initialize the array by filling in the initialization values in {}.

Specify array length

Specify len, the length of the array, and fill in {} with initialization values, which are assigned in descending order of indices.

The number of initialized values cannot exceed length len. When the number of initialized values is less than len, the uninitialized position is equal to the default value 0.

// The array length is 5, the first two numbers are initialized, and the uninitialized bit is 0
b := [5]int{1.2} 
for index, val := range b {
	fmt.Printf("Subscript = %d, value = %d\n", index, val)
}

/* Output: subscript = 0, value = 1, value = 2, value = 0, subscript = 3, value = 0, value = 4, value = 0 */
Copy the code

We can also initialize the array with {index1: a, index2: b}, specifying the array index and corresponding subscript value. The value of the unspecified subscript position is equal to the default value 0:

// Initialize by array index
// d[0] = 1, d[2] = 3
d := [5]int{0: 1.2: 3} 
for index, val := range d {
	fmt.Printf("Subscript = %d, value = %d\n", index, val)
}

/* Output: subscript = 0, value = 1, value = 0, value = 2, value = 3, value = 0, value = 4, value = 0 */
Copy the code

Implied array length

You can also use […] instead of specifying the length of the array. Instead. The array can be initialized in the same way as when specifying the length of the array.

When passing an initializer with {a, b, c}, Go determines the length of the array by initializing the number of elements:

// Determine the array length by passing the initialization value
// Pass 5 elements, array length 5
c := [...]int{1.2.3.4.5}  
for index, val := range c {
	fmt.Printf("Subscript = %d, value = %d\n", index, val)
}
/* Output: subscript = 0, value = 1, value = 2, value = 3, value = 3, value = 4, value = 5 */
Copy the code

If we initialize the array by specifying its index and corresponding value, the length of the array is equal to the maximum number of indexes + 1:

// The maximum index is 9, so the array length is 10 e := [... int{9: 10}for index, val := range e {
	fmt.Printf("Subscript = %d, value = %d\n", index, val) } /* Output: So if I have zero, I have zero if I have one, I have zero if I have two, I have zero if I have three, I have zero if I have five, I have zero if I have seven, I have zero if I have eight, Value = 0 subscript = 9, value = 10 */Copy the code

Array traversal

Use for in Go… Range Traversal number group:

// I is the array index
for i, _ := range arr1 {
    // do something
}
Copy the code

Arrays are value types

Unlike C/C++, arrays in Go are value types. In this way, assignments and arguments copy the entire array, not the pointer.

We initialize an array A, assign it to array B, and print the values and Pointers to both:

a := [5]int{1, 2, 3, 4, 5}
b := a
fmt.Printf("Array A - value: %v, pointer: %p\n", a, &a)
fmt.Printf("Array B - value: %v, pointer: %p\n", b, &b) /* Output: array A - value: [1 2 3 4 5], pointer: 0xC00001e0F0 Array B - value: [1 2 3 4 5], pointer: 0xC00001E120 */Copy the code

As you can see, the value is the same, but the memory address is different, indicating that the entire array was copied during assignment.

Let’s look at the example of passing parameters.

If there is any transmitA function, pass in the array a that we initialized earlier:

func main(a) {
	a := [5]int{1.2.3.4.5}
	fmt.Printf("Array A - value: %v, pointer: %p\n", a, &a)
	
	// Pass array A to the function
	transmitA(a)
}

func transmitA(a [5]int) {
	fmt.Printf("Array a-value of passed function: %v, pointer: %p\n", a, &a)
}

/* Output: array a - value: [1 2 3 4 5], pointer: 0xC00001e0f0 Array A - value: [1 2 3 4 5], pointer: 0xC00001e150 */
Copy the code

As you can see from the output, the values are still the same, but the memory address is different. This means that the array is copied when the parameter is passed.

Array Pointers and pointer arrays

Array Pointers and array Pointers sound like a bit of a mouthful, so let’s expand:

  • Array pointer: pointer to array
  • Pointer array :(filled) array of Pointers

That is, an array pointer is a pointer, it points to an array; And the pointer array is an array, it’s filled with Pointers.

Pointer to an array

Declare an array A and assign its address to arrayPointer. So arrayPointer is a pointer to array A that is of type *[5]int.

a := [5]int{1.2.3.4.5}
// Assign the address of array A to arrayPointer
// arrayPointer is a pointer to an array of type *[5]int
arrayPointer := &a
fmt.Println(arrayPointer)

/* Output:
&[1 2 3 4 5]
*/
Copy the code

Pointer to an array

Initialize the array pointerArray, passing the initialization values as the memory addresses of the integers m and n (&m and &n). Then pointerArray is an array of Pointers to int, which is of type [2]*int.

m := 1
n := 2
// Initialize the pointerArray, passing in the addresses of m and n
// pointerArray contains integer addresses. PointerArray is an array of Pointers
pointerArray := [2] *int{&m, &n}
fmt.Println(pointerArray)

/* Output:
[0xc0000aa000 0xc0000aa008]
*/
Copy the code

conclusion

  • The length of the array is fixed, and you need to “express” or “imply” the length of the array during initialization
  • The length of an array is part of the array type.[2]int[100]intIt’s an array of different types
  • usefor ... rangeThrough the array
  • In the Go language, arrays are value types, and arrays are copied both by assignment and by passing parameters
  • An array pointer is a pointer to an array
  • A pointer array is an array that holds Pointers

The resources

  • Go Study Notes

Review past

  • Talk about character representation and string traversal in Go
  • Simple to understand: take 🌰 to interpret source code, inverse code and complement code
  • Talk about object-oriented programming in Go

If you think the article is well written, please do me two small favors:

  1. Like and follow me to get this article seen by more people
  2. Pay attention to the public number “programming to save the world”, the public number focuses on programming foundation and server research and development, you will get the first time the new article push ~

Your encouragement is the biggest motivation for my creation. Thank you all!