The slices.DeleteFunc function in Golang is part of the slices package, introduced in Go 1.21 as part of the standard library. This function allows you to remove elements from a slice based on a custom deletion function. It is particularly useful when you need to filter out elements that meet certain criteria, rather than just deleting by index.
Table of Contents
- Introduction
slices.DeleteFuncFunction Syntax- Examples
- Basic Usage
- Deleting Elements Based on Condition
- Deleting Structs with Specific Field Values
- Real-World Use Case Example
- Conclusion
Introduction
The slices.DeleteFunc function provides a flexible way to delete elements from a slice by applying a custom function. This function is called for each element in the slice, and if it returns true, that element is removed. This makes it easy to clean up slices by removing unwanted elements based on complex conditions.
slices.DeleteFunc Function Syntax
The syntax for the slices.DeleteFunc function is as follows:
func DeleteFunc[S ~[]E, E any](s S, del func(E) bool) S
Parameters:
s S: The slice from which you want to delete elements.del func(E) bool: A custom function that defines the condition for deleting elements. It should returntruefor elements that should be deleted.
Returns:
S: A new slice with the elements that do not satisfy the deletion condition.
Behavior:
- Conditional deletion: The function iterates over the slice and applies the custom deletion function to each element. If the function returns
truefor an element, that element is removed from the resulting slice.
Examples
Basic Usage
This example demonstrates how to use slices.DeleteFunc to remove even numbers from a slice.
Example
package main
import (
"fmt"
"slices"
)
func main() {
// Define a slice of integers
numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
// Define a custom function to delete even numbers
deleteEven := func(n int) bool {
return n%2 == 0
}
// Use slices.DeleteFunc to remove even numbers
newSlice := slices.DeleteFunc(numbers, deleteEven)
// Print the resulting slice
fmt.Println("After deletion:", newSlice)
}
Output:
After deletion: [1 3 5 7 9]
Explanation:
- The
slices.DeleteFuncfunction removes all even numbers from thenumbersslice by applying thedeleteEvenfunction.
Deleting Elements Based on Condition
This example shows how to use slices.DeleteFunc to remove strings that are shorter than 5 characters from a slice.
Example
package main
import (
"fmt"
"slices"
)
func main() {
// Define a slice of strings
words := []string{"apple", "fig", "banana", "pear", "grape"}
// Define a custom function to delete strings shorter than 5 characters
deleteShort := func(s string) bool {
return len(s) < 5
}
// Use slices.DeleteFunc to remove short strings
newSlice := slices.DeleteFunc(words, deleteShort)
// Print the resulting slice
fmt.Println("After deletion:", newSlice)
}
Output:
After deletion: [apple banana grape]
Explanation:
- The
slices.DeleteFuncfunction removes all strings that are shorter than 5 characters from thewordsslice by applying thedeleteShortfunction.
Deleting Structs with Specific Field Values
This example demonstrates how slices.DeleteFunc can be used to remove structs from a slice based on specific field values.
Example
package main
import (
"fmt"
"slices"
)
type Person struct {
Name string
Age int
}
func main() {
// Define a slice of Person structs
people := []Person{
{"Alice", 30},
{"Bob", 40},
{"Charlie", 25},
{"Dave", 40},
}
// Define a custom function to delete people who are 40 years old
deleteAge40 := func(p Person) bool {
return p.Age == 40
}
// Use slices.DeleteFunc to remove people who are 40 years old
newSlice := slices.DeleteFunc(people, deleteAge40)
// Print the resulting slice
fmt.Println("After deletion:", newSlice)
}
Output:
After deletion: [{Alice 30} {Charlie 25}]
Explanation:
- The
slices.DeleteFuncfunction removes allPersonstructs whereAgeis40by applying thedeleteAge40function, resulting in a slice with only those who are not 40 years old.
Real-World Use Case Example: Filtering Outdated Data
A practical use case for slices.DeleteFunc is filtering out outdated or irrelevant data from a list of records.
Example: Deleting Outdated Records
package main
import (
"fmt"
"time"
"slices"
)
type Record struct {
Name string
Timestamp time.Time
}
func main() {
// Define a slice of records with timestamps
records := []Record{
{"Record1", time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)},
{"Record2", time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)},
{"Record3", time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC)},
}
// Define a custom function to delete records older than 2024
deleteOutdated := func(r Record) bool {
return r.Timestamp.Year() < 2024
}
// Use slices.DeleteFunc to remove outdated records
newSlice := slices.DeleteFunc(records, deleteOutdated)
// Print the resulting slice
fmt.Println("After deletion:", newSlice)
}
Output:
After deletion: [{Record2 2024-01-01 00:00:00 +0000 UTC}]
Explanation:
- The
slices.DeleteFuncfunction removes records with timestamps before the year 2024, leaving only the most recent data in the slice.
Conclusion
The slices.DeleteFunc function in Go is used for removing elements from slices based on custom conditions. It is particularly useful when dealing with complex types or when the criteria for deletion require custom logic. By using slices.DeleteFunc, you can efficiently manage and clean up your data in Go applications, ensuring that only the relevant elements remain in your slices.