In Go, generic functions can interact with non-generic code by using type assertions, type aliases, or empty interfaces. Type assertions allow values to be converted to specific types; type aliases create generic aliases of existing types; and empty interfaces can represent variables of any type. Through these methods, generic functions can accept or return values of non-generic types, enabling data processing across different types.
How to make generic functions interact with non-generic functions in Go
Go Generics have been introduced since Go 1.18 , opens the door to reusing type and algorithm code. But how does new generic code interact with existing, non-generic code?
Using type assertions
Type assertions provide a way to convert an interface into a value of a specific type. This can be done using a switch statement:
func AnyToString(any interface{}) string { switch myString := any.(type) { case string: return myString default: return "Unknown" } }
This function attempts to convert an arbitrary value to a string, returning "Unknown" if it is not a string.
Using type aliases
Type aliases allow you to create aliases for existing types. This allows us to create a generic alias for a non-generic type:
type MyString string func GenericFunc[T MyString](t T) {}
Now, we can use the non-generic type MyString
in a generic function GenericFunc
:
GenericFunc(MyString("Hello"))
Use empty interface
The empty interface can represent any type of variable. This allows us to create generic functions that accept or return any type of value:
func GenericEmptyInterfaceFunc(empty interface{}) {}
We can call this function with any type of value:
GenericEmptyInterfaceFunc(10) GenericEmptyInterfaceFunc("Hello")
Practical example: Implementing generic sorting
Let's demonstrate the interaction of generic code with non-generic code by sorting a list.
// Sort is a generic function that sorts a slice of any type that implements sort.Interface. func Sort[T sort.Interface](s []T) { sort.Sort(s) } // IntSlice implements sort.Interface for a slice of int. type IntSlice []int func (s IntSlice) Len() int { return len(s) } func (s IntSlice) Less(i, j int) bool { return s[i] < s[j] } func (s IntSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } // StringSlice implements sort.Interface for a slice of string. type StringSlice []string func (s StringSlice) Len() int { return len(s) } func (s StringSlice) Less(i, j int) bool { return s[i] < s[j] } func (s StringSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } func main() { intSlice := IntSlice{10, 5, 7, 3, 11} Sort(intSlice) fmt.Println(intSlice) // Output: [3 5 7 10 11] stringSlice := StringSlice{"Hello", "World", "Go", "Golang"} Sort(stringSlice) fmt.Println(stringSlice) // Output: [Go Golang Hello World] }
This code demonstrates how to use the generic function Sort
to sort different lists of values based on a custom type.
The above is the detailed content of How do generic functions interact with existing non-generic functions in Golang?. For more information, please follow other related articles on the PHP Chinese website!