Introduction
A collection is a set of non-repeating elements, where the elements can be of any type.
At the same time, gset supports optional concurrent safety parameters for concurrent-safe scenarios.
Use Cases:
Collection operations.
Usage:
import "github.com/gogf/gf/v2/container/gset"
Interface Documentation: https://pkg.go.dev/github.com/gogf/gf/v2/container/gset
NilChecker and Typed Nil Support
- Feature Overview: In the generic version,
gsetprovidesNilCheckerfunctions for the generic set typeTSet[T]to customize "which elements should be considered nil", for more precise handling of typed nil scenarios when containing pointers, interfaces, and other types. - Usage: You can create sets via
NewTSetWithChecker,NewTSetWithCheckerFrom, or register at runtime by callingRegisterNilCheckerwith afunc(T) booldetermination function. Lazy loading/conditional write methods (such asAddIfNotExist*series) will call this function before actually writing, and usually won't add the element to the set when determined as nil. - Compatibility: If
NilCheckeris not set, it maintains consistency with historical versions, defaulting toany(v) == nilfor determination, and typed nil behavior won't change.
Example:
type Student struct {
Name string
}
// Treat *Student(nil) as "invalid element", won't add to set
set := gset.NewTSetWithChecker[*Student](func(s *Student) bool {
return s == nil
}, true)
ok := set.AddIfNotExist(nil)
fmt.Println(ok) // false
fmt.Println(set.Size()) // 0
Documentation
📄️ Set - Usage
Using set types and their basic operation methods in the GoFrame framework, including creating, adding, deleting, and traversing sets, as well as exploring advanced operations such as intersection, difference, union, and complement. In addition, the article also provides detailed explanations on inclusion judgment, set item popping, subset judgment, conditional writing, and demonstrates JSON serialization and deserialization with code examples.
📄️ Set - Performance
Performance test results of collection types in the GoFrame framework, including benchmark tests for set operations with various data types such as integers, any data type, and strings. These benchmarks illustrate the performance metrics of different set operations, helping developers to optimize code performance and improve efficiency when building high-performance applications using the GoFrame framework.
📄️ Set - Methods
Implement basic set operations using the GoFrame library, including creating sets, adding elements, set operations, element checking and removal, set iteration, among others. It also provides concrete code examples to help understand and apply these methods.