Overview
- Slicing is a dynamic array
- Automatically change size as needed
- Compared to arrays, the length of slices can be modified at runtime
grammar
1. Create and initialize slices
make
Create slices using the built-in function make():
var slice []type = make([]type, len, cap) //Abbreviation: slice := make([]type, len, cap)
Literal
var variable name []type
slice1:=[]string{"Zhang San","Li Si"} // Slice strings with 5 elements in length and capacityslice2 := []int{10, 20, 30} // Integer slices with 3 elements in length and capacity
2. Use slices
Assign and slice
Use the [] operator to change an element, for example:
// Create an integer slice// Its capacity and length are 5 elementsslice1:=[]string{"Zhang San","Li Si","Wang Wu","Maliu","Old Seven"} // Change the value of the element with index 1slice1[1] = "Xiao Zhang San"
Create slices using slices
// Create an integer slice// Its length and capacity are 5 elementsslice1:=[]string{"Zhang San","Li Si","Wang Wu","Maliu","Old Seven"} // Create a new slice// Its length is 3 elements and its capacity is 3 elementsnewSlice:=slice1[2:5]
Slice growth
Use append to add elements to the slice while increasing the length and capacity of the slice.
// Create an integer slice// Its length and capacity are 5 elementsslice1:=[]string{"Zhang San","Li Si","Wang Wu","Maliu","Old Seven"} // Use the original capacity to allocate a new element// Assign the new element to 60newSlice:=append(slice1,"I'm new")
Traversal slices
Iterate the slice using for range
slice1 := []string{"Zhang San", "Li Si", "Wang Wu", "Maliu", "Old Seven"} for k, v := range slice1 { (k, v) }
Iterate the slice using for loop
slice1 := []string{"Zhang San", "Li Si", "Wang Wu", "Maliu", "Old Seven"} for i := 0; i < len(slice1); i++ { (i,slice1[i]) }
Summarize
- The default start position of slice is 0, ar[:n] is equivalent to ar[0:n]
- slice is a reference type, a pointer to an array
- Cannot use == to determine whether two slices are given to all the same elements
- >Judge whether slice is empty, use len(s) == 0 instead of s == nil
Total examples
package main import ( "fmt" ) func main() { //1. Create slices var slice []int = make([]int, 3) (slice) slice1 := []string{"Zhang San", "Li Si", "Wang Wu", "Maliu", "Old Seven"} (slice1) slice2 := []int{10, 20, 30} (slice2) //2. Use slices //Use the [] operator to change an element slice1[1] = "Xiao Zhang San" (slice1) //Create slices using slices [Subscript starts from 0] newSlice := slice1[0:2] (newSlice) //Slice growth newSlice = append(slice1, "I'm new") (newSlice) // Use for range to iterate the slice [k: represents the subscript, v represents the value] for k, v := range slice1 { (k, v) } // Iterate the slice using for loop for i := 0; i < len(slice1); i++ { (i,slice1[i]) } }
Example 1: Are the two slices equal?
package main import ( "fmt" "reflect" ) func main() { // Are the two slices equal? slice1 := []string{"Zhang San", "Li Si", "Wang Wu", "Maliu"} slice2 := []string{"Zhang San", "Li Si", "Wang Wu", "Maliu"} if (slice1, slice2) { ("Two slices are equal") } else { ("Two slices are not equal") } }
Example 2: Whether the two numbers are included
package main import ( "fmt" "sort" "strings" ) func main() { slice1 := []string{"Zhang San", "Li Si", "Wang Wu", "Maliu", "Old Seven"} (slice1) target := "Li Si" i := (len(slice1), func(i int) bool { return slice1[i] >= target }) if (slice1[i], target) { (target, "Existence, its subscript is", i) } else { ("Not exists", target) } }
The above is a detailed explanation of the creation and initialization examples of Go basic slices. For more information about Go language slices, please pay attention to my other related articles!