An array of

Introduction of the array

An array is a build-in type. It is a collection of data of the same type. It is a value type that accesses the element value through a subscript index starting at 0. The length is fixed after initialization and cannot be changed. When passed as a parameter to a method, a set of numbers is copied instead of referring to the same pointer. The length of an array is also part of its type, and is obtained by the built-in function len(array).


  • Arrays in Go are value types. In other words, if you assign an array to another array, you are essentially copying the entire array
  • If the array in Go takes arguments to the function, the argument actually passed is a copy of the array, not a pointer to the array. I want to distinguish this from C. Therefore, passing an array as an argument to a function in Go is not as efficient as passing a pointer.
  • The length of the array is also part of Type, which means that [10]int is different from [20]int

To define an array


Var arr [3]int arr = [3]int{1, 2, 3} var arr [3]int arr = [3]int{1, 2, 3} Var arr2 [2]string = [2]string{"hello"."world"} // In the array definition, if "... "appears in the array length position. Arr3 := [...] Int {11,22} // print the array ftt.println (arr) ftt.println (arr1) ftt.println (arr2) ftt.printfln (arr3)Copy the code

Results:

[1 2 3]
[3 6]
[hello world]
[1 4] Copy the code

Array traversal


arr := [3]int{1, 2, 3}
//fortraversefor i := 0; i < len(arr); i++ {
	fmt.Println(arr[i])
}

fmt.Println("分隔符。。。。。。。。。"// work with range traversalfor k, v := range arr {
	fmt.Println(k, v)
}Copy the code

Results:

1, 2, 3... 0, 1, 1, 2, 2, 3Copy the code

slice

Slice profile

Arrays are immutable in length, and such collections are not very useful in certain scenarios. Go provides a flexible, powerful built-in type, Slices (” dynamic arrays “), whose lengths are not fixed compared to arrays and whose appending of elements may increase the size of the Slices. There are two concepts in slice: len length and CAP capacity. Length refers to the maximum subscript +1 that has been assigned a value, which can be obtained through the built-in function Len (). Capacity refers to the maximum number of elements that a slice can currently hold, which can be obtained by the built-in function cap(). A slice is a reference type, so the same pointer is referenced when a slice is passed, and changing the value will affect other objects

Slice initialization

1. Generate a new slice from an array or slice

By default, a slice points to a contiguous memory region, either an array or the slice itself

Slice [start position: end position]

The syntax is as follows:

  • Slice: indicates the target slice object.
  • Start position: index of the corresponding target slice object;
  • End position: the end index of the corresponding target slice. Items identified by the end index are not included in the slice.
Arr := [3]int{1, 2, 3} FMT.Println(arr[1:2]) // subscript 1 to subscript 2, but not include the value of subscript 2Copy the code

Results:

[2]Copy the code

2. Declare slices directly

Var numList []int // Declare an empty slice. var numListEmpty = []int{ Println(strList, numList, numListEmpty) // The following syntax cannot be used to define slices //testPrintln(len(strList), len(numList), Println(strList == nil) fmt.Println(numList == nil) fmt.Println(numListEmpty == nil) fmt.Println(numListEmpty == nil)Copy the code

Results:

[] [] 0 0 0true
true
false Copy the code

3. Construct slices using the make() function

If you need to create a slice dynamically, you can use the make() built-in function in the following format:

make( []Type, size, cap )

a := make([]int, 2)
b := make([]int, 2, 10)
fmt.Println(a, b)
fmt.Println(len(a), len(b))Copy the code

Results:

[0 0] [0 0]
2 2 Copy the code

Both A and B are pre-allocated slices of 2 elements, but the internal storage space of B has been allocated 10, but 2 elements are actually used. The capacity doesn’t affect the number of elements, so len is equal to 2 for both a and B.

Note: memory allocation must occur in the slices generated by make() function, but the slices given start and end positions (including slice reset) only point the new slice structure to the allocated memory region, and no memory allocation will occur when the start and end positions are set.

Slice assignment and reference

A slice is a reference type, so when a reference changes the value of an element in it, all other references change that value

Arr := [3]int{1,2,3} var aSlience []int aSlience = arr[1:2] fmt.println ("Array change before ---------")
fmt.Println(arr)
fmt.Println(aSlience)

fmt.Println("Array changed ---------") arr[1] = 1000 // Change the second element of the array to fmt.println (arr) fmt.println (aSlience)Copy the code

Results:

Array change before -- -- -- -- -- -- -- -- -- (1 2 3] [2] array after change -- -- -- -- -- -- -- -- -- (1 1000 3] [1000]Copy the code

Note: The difference between a slice and an array is: when declaring an array, the length of the array is specified in square brackets. Automatically calculates the length, while declaring slices without any characters in square brackets.

Reference article:

Blog.csdn.net/belalds/art…

C.biancheng.net/view/27.htm…

C.biancheng.net/view/26.htm…