Slicing is a very frequently used type of aggregation in the Go language, which represents a variable length sequence with a base reference to an array object. A slice consists of three parts: pointer, length, and capacity. Pointer to the memory address of the underlying array element corresponding to the first element of the slice itself.
The type of slice is declared as follows:
type slice struct {
array unsafe.Pointer
len int
cap int
}
Copy the code
Multiple slices can share data from the underlying array, and the array ranges referenced may overlap. Using slice this feature we can in the original memory space to slice to heavy, screening, and for the operations, so you don’t have to declare a pointer to a new slice to store the result of memory, so as to save the memory space and extend the underlying array of consumption, in the section length enough when the effect is very significant.
The following examples are operations that slice the memory space of the underlying array. Note that these operations also change the underlying array as they generate new slices on the underlying array.
Deletes the element at the specified location
The following function removes the element at index position I from the original slice
func remove(slice []int, i int) []int {
copy(slice[i:], slice[i+1:)return slice[:len(slice)- 1]}func main(a) {
s := []int{5.6.7.8.9}
fmt.Println(remove(s, 2)) // "[5 6 8 9]"
}
Copy the code
The built-in copy function makes it easy to copy one slice to another of the same type.
Filter element
The following function filters the slice elements that meet the conditions from the input source slice and returns a new slice composed of the elements that meet the conditions.
type funcType func(T) bool// represents the filter logic function, which can be implemented on demandfunc filter(a []T, f funcType) []T {
b := a[:0]
for _, x := range a {
if f(x) {
b = append(b, x)
}
}
return b
}
Copy the code
Inversion section
func reverse(a []T) []T {
for i := len(a)/2- 1; i >= 0; i-- {
opp := len(a)- 1-i
a[i], a[opp] = a[opp], a[i]
}
return a
}
Copy the code
Grouping slice
The following function takes a source slice actions of type []int and returns a nested slice grouped by the specified length. []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, batchSize = 3, batchSize = [[0 1 2] [3 4 5] [6 7 8] [9]]
func chunk(actions []int, batchSize int) []int {
var batches [][]int
for batchSize < len(actions) {
actions, batches = actions[batchSize:], append(batches, actions[0:batchSize:batchSize])
}
batches = append(batches, actions)
return batches
}
func main(a) {
actions := []int{0.1.2.3.4.5.6.7.8.9}
batchSize := 3
chunks = chunk(actions, batchSize)
//chunks are [[0 1 2] [3 4 5] [6 7 8] [9]]
}
Copy the code
Here, by the way, the full slice expression looks like this:
input[low:high:max]
Copy the code
The final effect of Max is that the cap (capacity) of the generated slice is max-low.
In situ deweighting (for comparable slice types only)
import "sort"
func main(a) {
in := []int{3.2.1.4.3.2.1.4.1} // any item can be sorted
sort.Ints(in)
j := 0
for i := 1; i < len(in); i++ {
if in[j] == in[i] {
continue
}
j++
in[j] = in[i]
}
result := in[:j+1]
fmt.Println(result) // [1 2 3 4]
}
Copy the code
Some of the examples in this article are from golang’s GitHub Wiki, which provides tips on how to use slicing. For more information, visit Golang’s GitHub Wiki at github.com/golang/go/w…