This is the 8th day of my participation in Gwen Challenge
Hi, I’m @Luo Zhu
This article was first published on luo Zhu’s official website
This article was translated from Golang Tutorial Series
This article synchronizes in the public account “luo Zhu early teahouse”, reprint please contact the author.
Creation is not easy, form a habit, quality three even!
Slicing is a convenient, flexible, and powerful wrapper over arrays. Slicing does not own any data of its own. They are just references to existing arrays.
Create a slice
A slice with elements of type T is represented by []T.
package main
import (
"fmt"
)
func main(a) {
a := [5]int{76.77.78.79.80}
var b []int = a[1:4] // Create a slice from A [1] to a[3]
fmt.Println(b)
}
Copy the code
Run in playground
Syntax A [start:end] creates a slice from array A, starting at index start through index end-1. So in the above program, A [1:4] creates a slice of array A from indexes 1 through 3. Therefore, the value of slice B is [77 78 79] ‘.
Let’s look at another way to create slices.
package main
import (
"fmt"
)
func main(a) {
c := []int{6.7.8} // Create an array and return the index of the slice
fmt.Println(c)
}
Copy the code
Run in playground
In the above program, c := []int{6, 7, 8} creates an array of three integers and returns a slice reference stored in C.
Modify a slice
A slice does not own any data of its own. It’s just a representation of the underlying array. Any changes made to the slice are reflected in the underlying array.
package main
import (
"fmt"
)
func main(a) {
darr := [...]int{57.89.90.82.100.78.67.69.59}
dslice := darr[2:5]
fmt.Println("array before",darr)
for i := range dslice {
dslice[i]++
}
fmt.Println("array after",darr)
}
Copy the code
Run in playground
In the above program, we create a Dslice from indexes 2, 3, and 4 of the array. The for loop increments the values of these indexes by 1. When we print the array after the for loop, we can see that the change in the slice is reflected in the array. The output of this program is
array before [57 89 90 82 100 78 67 69 59]
array after [57 89 91 83 101 78 67 69 59]
Copy the code
When slices share the same underlying array, changes to each slice are reflected in the array.
package main
import (
"fmt"
)
func main(a) {
numa := [3]int{78.79 ,80}
nums1 := numa[:] // Create a fragment containing all the elements of the array
nums2 := numa[:]
fmt.Println("array before change 1",numa)
nums1[0] = 100
fmt.Println("array after modification to slice nums1", numa)
nums2[1] = 101
fmt.Println("array after modification to slice nums2", numa)
}
Copy the code
Run in playground
In NUMA [:], start and end values are missing. The default values for start and end are 0 and len(numa), respectively. Two slices nums1 and nums2 share the same array. The output of this program is
array before change 1 [78 79 80]
array after modification to slice nums1 [100 79 80]
array after modification to slice nums2 [100 101 80]
Copy the code
From the output, it is clear that when slices share the same array. Changes made to slices are reflected in the array.
Length and capacity of slice
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.
Let’s write some code to understand this better.
package main
import (
"fmt"
)
func main(a) {
fruitarray := [...]string{"apple"."orange"."grape"."mango"."water melon"."pine apple"."chikoo"}
fruitslice := fruitarray[1:3]
fmt.Printf("length of slice %d capacity %d".len(fruitslice), cap(fruitslice)) // The length of the cut is 2 and the capacity is 6
}
Copy the code
Run in playground
In the above program, fruitslice is created by fruitArray indexes 1 and 2. So fruitslice has a length of 2.
Fruitarray has a length of 7. Fruiteslice is created from index 1 of the FruitArray. So the capacity of fruitslice is the number of elements in fruitArray ‘starting from index 1, which is orange, which is 6. So fruitslice ‘has a capacity of 6. Program prints length of slice 2 capacity 6.
A slice can be re-cut to its capacity. Any portion that exceeds this capacity will result in a runtime error in the program.
package main
import (
"fmt"
)
func main(a) {
fruitarray := [...]string{"apple"."orange"."grape"."mango"."water melon"."pine apple"."chikoo"}
fruitslice := fruitarray[1:3]
fmt.Printf("length of slice %d capacity %d\n".len(fruitslice), cap(fruitslice)) // The length is 2 and the capacity is 6
fruitslice = fruitslice[:cap(fruitslice)] // Reslice the slice until it reaches its capacity.
fmt.Println("After re-slicing length is".len(fruitslice), "and capacity is".cap(fruitslice))
}
Copy the code
Run in playground
In the above program, fruitslice is re-fited into its capacity. The output of the above program.
length of slice 2 capacity 6
After re-slicing length is 6 and capacity is 6
Copy the code
Use make to create slices
Func make([]T, len, cap) []T creates a slice by passing the type, length, and capacity. The capacity parameter is optional and defaults to length. The make function creates an array and returns a reference to its slices.
package main
import (
"fmt"
)
func main(a) {
i := make([]int.5.5)
fmt.Println(i)
}
Copy the code
Run in playground
These values default to zero when creating a slice using make. The above program will print [0 0 0 0 0].