Home > Article > Backend Development > Special use cases and techniques of generics in golang
Special use cases and tricks for generics in Go Use empty type interfaces for dynamic type checking to check runtime types. Use generic type parameters in collections to create containers of diverse types. Implement generic methods to perform common operations for different types of parameters. Use type constraints to implement type-specific generics and customize operations for specified types.
Special use cases and techniques of generics in Go
Generics introduce novel features that make writing flexible and efficient Code becomes possible. This article will explore special use cases and techniques for generics in Go.
1. Use empty type interface for dynamic type checking
any
type can represent any type. This allows us to perform dynamic type checking based on types determined at runtime.
func isString(v any) bool { _, ok := v.(string) return ok } func main() { x := "hello" y := 10 fmt.Println(isString(x)) // true fmt.Println(isString(y)) // false }
2. Using generic types on collections
Generic type parameters can be used in collection types to create a container of diverse types.
type Stack[T any] []T func (s *Stack[T]) Push(v T) { *s = append(*s, v) } func (s *Stack[T]) Pop() T { if s.IsEmpty() { panic("stack is empty") } v := (*s)[len(*s)-1] *s = (*s)[:len(*s)-1] return v } func main() { s := new(Stack[int]) s.Push(10) s.Push(20) fmt.Println(s.Pop()) // 20 fmt.Println(s.Pop()) // 10 }
3. Implement generic methods
Generic methods allow us to implement common operations for different types of parameters.
type Num[T numeric] struct { V T } func (n *Num[T]) Add(other *Num[T]) { n.V += other.V } func main() { n1 := Num[int]{V: 10} n2 := Num[int]{V: 20} n1.Add(&n2) fmt.Println(n1.V) // 30 // 可以使用其他数字类型 n3 := Num[float64]{V: 3.14} n4 := Num[float64]{V: 2.71} n3.Add(&n4) fmt.Println(n3.V) // 5.85 }
4. Use type constraints to implement type-specific generics
Type constraints limit the scope of generic types. It allows us to implement custom operations for specific types.
type Comparer[T comparable] interface { CompareTo(T) int } type IntComparer struct { V int } func (c *IntComparer) CompareTo(other IntComparer) int { return c.V - other.V } // IntSlice 实现 Comparer[IntComparer] 接口 type IntSlice []IntComparer func (s IntSlice) Len() int { return len(s) } func (s IntSlice) Less(i, j int) bool { return s[i].CompareTo(s[j]) < 0 } func (s IntSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } func main() { s := IntSlice{{10}, {20}, {5}} sort.Sort(s) fmt.Println(s) // [{5} {10} {20}] }
These special use cases and techniques demonstrate the power of generics in Go, allowing the creation of more general, flexible, and efficient code.
The above is the detailed content of Special use cases and techniques of generics in golang. For more information, please follow other related articles on the PHP Chinese website!