Home >Backend Development >Golang >A preliminary exploration of generics in Go1.18
#The function we want to implement today is: sum all the values in the map.
How to implement it if there is no generic type? Because value may have different types, such as: int64, float64, etc., without generics, different types need to be implemented through different functions, so we generally implement it as follows:
func SumInts(m map[string]int64) int64 { var s int64 for _, v := range m { s += v } return s } func SumFloats(m map[string]float64) float64 { var s float64 for _, v := range m { s += v } return s }
The above Code, we have defined two functions:
1. SumInts() calculates the sum of value as int64 type; 2. SumFloats() calculates the sum of value as float64 type;
The above function only It is suitable for int64 and float64 types. If we still want to calculate int, float32, etc., we have to re-copy the above function and modify the type.
Isn’t it a little troublesome to do this, and the code is very bloated? Not to mention, there are many similar codes in the previous standard package, such as the Sort package (sort.go) of the standard library in order to implement different types of slicing. Sorting, IntSlice, Float64Slice, StringSlice are defined.
Because the previous Go version did not support generics, it can only be implemented in this way, which is also the simplest method.
With generics, how can we implement a function to calculate the sum of values for different types of int64 and float64?
#In this section we use a generic function to receive a map whose value is int64 type as a parameter, and also Can receive a map whose value is float64 type as parameter.
The complete code is as follows:
func Sum[K comparable, V int64 | float64](m map[K]V "K comparable, V int64 | float64") V { var s V for _, v := range m { s += v } return s } func main() { ints := map[string]int64{ "first": 1, "second": 2, } fmt.Println(Sum[string, int64](ints "string, int64")) floats := map[string]float64{ "first": 35.98, "second": 26.99, } fmt.Println(Sum[string, float64](floats "string, float64")) }
In the above code, we defined the Sum() function, which is a generic function. You can see that the difference from the ordinary function is that the function name and There is a set of square brackets [] between the function parameter list. There are two parameters K and V in the square brackets. They are called type parameters in the Go language. Following K and V are type restrictions. , where comparable is pre-declared by Go language and represents any type that can perform == and != operations. The type restriction of V is one of int64 and float64. The function parameter is m, the type is map[K]V, and the return type is V.
Calling a function is also a little different from calling an ordinary function. There is a set of square brackets [] between the function name and the actual parameters, indicating the type name. For example, when the Sum() function is called for the first time, the type names are string and int64 respectively. Then when the function is executed, the corresponding types of K and V are string and int64 respectively.
第二次调用该 Sum() 函数时,K、V 的类型分别是 string 和 float64。
通过泛型编程,我们就可以实现一个函数处理多种数据类型。
执行上面的代码输出:
3 62.97
The above is the detailed content of A preliminary exploration of generics in Go1.18. For more information, please follow other related articles on the PHP Chinese website!