Generic structure encapsulation slicing method
- To encapsulate the addition, deletion, modification, checking, length and size of slices, ForEach (traversing slices)
- Use pointer receiver function to encapsulate and modify its original slice data
- Declare a
SliceData
Generic structure
type SliceData[T any] struct { data []T size int }
Abovedata
is the element of slice,size
Indicates the length of the slice
Push Add
Push
Methods are used to increase the element of the slice and return the length of the slice after the increase
func (sd *SliceData[T]) Push (elements ...T) int { = append (, elements...) = len() return }
usePush
package main import "fmt" type SliceData[T any] struct { data []T size int } func main() { // Declare a []int slice sliceData := SliceData[int]{ data: []int{1, 2, 3, 4}, size: 4, } // Add elements using the Push method index := (5, 6, 7, 8) (index, )// 8 [1 2 3 4 5 6 7 8] } func (sd *SliceData[T]) Push(elements ...T) int { = append(, elements...) = len() return }
Print result:
8 [1 2 3 4 5 6 7 8]
Remove
Delete the element of the specified index and return the element
func (sd *SliceData[T]) Remove(index int) T { element := [index] = append ([:index], [index+1:]...) -= 1 return element }
useRemove
package main import "fmt" type SliceData[T any] struct { data []T size int } func main() { sliceData := SliceData[int]{ data: []int{1, 2, 3}, size: 3, } // Delete the 0th element element := (0) (element, , ) // 1 [2 3] 2 } func (sd *SliceData[T]) Remove(index int) T { element := [index] = append([:index], [index+1:]...) -= 1 return element }
Print result:
1 [2 3] 2
Set Change
Modify the value of the specified index and return two values: the original index value and the modified value
func (sd *SliceData[T]) Set (index int, element T) (oldValue, newValue T) { oldValue = [index] newValue = element [index] = element return oldValue, newValue }
use:Set
package main import "fmt" type SliceData[T any] struct { data []T size int } func main() { sliceData := SliceData[int]{ data: []int{1, 2, 3}, size: 3, } newValue, oldValue := (0, 100) (newValue, oldValue, ) // 1 100 [100 2 3] } func (sd *SliceData[T]) Set(index int, element T) (oldValue, newValue T) { oldValue = [index] newValue = element [index] = element return oldValue, newValue }
Print result:
1 100 [100 2 3]
Get check
Gets and returns the value of the specified index
func (sd *SliceData[T]) Get (index int) T { return [index] }
ForEach traversal slices
ForEach
Methods are used to traverse slices. It accepts a callback function with the callback parameters:index (index)
、element (element)
、slice (slice)
, execute the logic you defined inside the callback function.
func (sd *SliceData[T]) ForEach (cb func ( index int, element T, slice []T, )) T { for index, element := range { cb (index, element, ) } }
use:ForEach
package main import "fmt" type SliceData[T any] struct { data []T size int } func main() { sliceData := SliceData[int]{ data: []int{1, 2, 3}, size: 3, } (func(index int, element int, slice []int) { (index, element, slice) }) } func (sd *SliceData[T]) ForEach(cb func( index int, element T, slice []T, )) { for index, element := range { cb(index, element, ) } }
Print result:
0 1 [1 2 3]
1 2 [1 2 3]
2 3 [1 2 3]
Map
Map
The method accepts a callback function with the same parametersForEach
method. The difference is that you canelement
Make a modification and it returns a new slice.
package main import "fmt" type SliceData[T any] struct { data []T size int } func main() { sliceData := SliceData[int]{ data: []int{1, 2, 3}, size: 3, } newSlice := (func(index int, element int, slice []int) int { element += 1 return element }) (newSlice) } func (sd *SliceData[T]) Map(cb func( index int, element T, slice []T, ) T) []T { newSlice := make([]T, 0) for index, element := range { element = cb(index, element, ) newSlice = append(newSlice, element) } return newSlice }
Print result:
[2 3 4]
Size Get the length size
func (sd *SliceData[T]) Size () int { return }
GetSlice Gets the slice element
func (sd *SliceData[T]) GetSlice () []T { return }
The above is the detailed content of golang using generic structures to implement encapsulated slices. For more information about go encapsulated slices, please pay attention to my other related articles!