This is the 10th day of my participation in the August Text Challenge.More challenges in August

Len () and cap() functions

The length of a slice is the number of elements in the slice. The size of a slice is the number of elements in the underlying array starting with the index on which the slice was created

Slices are indexable and can be length obtained by the len() method. Slices provide a method cap() of several volumes that can measure how long slices can be up to

package main
import "fmt"
func main() {
    var num = make([]int, 2, 3)
    printSlice(num)
}

func printSlice(x []int) {
    fmt.Printf("len=%d cap=%d slice=%v\n", len(x), cap(x), x)   // 2, 3, [0 0]
}
Copy the code

Results:

// Len = 2, cap = 3, num = [0, 0]

Empty section

A slice defaults to nil with a length of 0 until initialized

Package main import "FMT" func main() {var num []int printSlice(num) if(num == nil) {FMT.Printf(" empty slice ")}} func printSlice(x []int) { fmt.Printf("len=%d cap=%d slice=%v\n", len(x), cap(x), x) }Copy the code

Results:

Len = 0, cap = 0, num = [], empty slice

demo2

Num := []int{0, 1, 2, 3, 4, 5, 6, 7, PrintSlice (num [1:4]=", num) printSlice(num [1:4]=", num) Num [and]). / / the default threshold is 0 FMT Println (" num [3] : = ", num [3] :) / / the default limit for len (s) FMT. Println (" num [4:] = ", num[4:]) num1 := make([]int, 0, Num2 := num[:2] printSlice(num2) // printSlice(num1) from index 0(included) to index 2(not included) num3 := Func printSlice(x []int) {FMT.Printf("len=%d cap=%d slice=%v\n", len(x), cap(x), x) }Copy the code

Results:

Append () and copy()

Append appends one or more elements to slice and returns a slice copy function of the same type as Slice. Copy copies elements from the source slice SRC to the destination DST and returns the number of copied elements

The append function changes the contents of the array referenced by slice, affecting other slices that reference the same array. But when there is no remaining space in slice ((cap-len) == 0), new array space is allocated dynamically. The returned slice array pointer points to the space, while the contents of the original array remain unchanged; Other slices that reference this array are not affected

The following code describes the copy method for copying slices and the Append method for appending new elements to slices

Package main import "FMT" func main() {var numbers []int printSlice(numbers) / 0) printSlice(numbers) /* Add an element to the slice */ numbers = append(numbers, 1) printSlice(numbers) /* Add multiple elements */ numbers = append(numbers, /* make([]int, len(numbers), (cap(numbers) *2) /* Copy (numbers) printSlice(numbers)} func printSlice(x []int){  fmt.Printf("len=%d cap=%d slice=%v\n",len(x),cap(x),x) }Copy the code

Results:

Note: There is no relation between numbers and numbers1. When numbers change, numbers do not change. That is, the copy method does not establish a link between two slices

The last

Just learning Golang, we hope everyone can supervise and work together

If you are interested, please follow me at EntrepreneurialG