Official instructions for using go generics:/doc/tutorial/generics
Update go to version 1.18 or above before using it:/doc/install
If you have used C++ or Java, you should be familiar with the concept of generics. (The following definition is excerpted from Baidu Encyclopedia)
Generic programming is a style or paradigm of programming language. Generics allow programmers to use some later-specified types when writing code in strongly typed programming languages, and specify these types as parameters when instantiated.
We can use a simple example to see how to use generics.
Before there are no generics, if we want to write a function of comparative size, because there is no concept of function overloading in go, we must create a comparison function for the types that need to be compared. However, in this way, there will be a lot of logically repeated code, and the difference is that the types of variables are different.
package main import "fmt" func MaxInt(a, b int) int { if a > b { return a } else { return b } } func MaxFloat32(a, b float32) float32 { func main() { var a, b int var c, d float32 a = 1 b = 2 c = 1.1 d = 1.4 (MaxInt(a, b)) (MaxFloat32(c, d))
So how should generics be used?
Go reserved a generic namedcomparable
, This is an official definition of a comparable type constraint
// comparable is an interface that is implemented by all comparable types // (booleans, numbers, strings, pointers, channels, arrays of comparable types, // structs whose fields are all comparable types). // The comparable interface may only be used as a type parameter constraint, // not as the type of a variable. //Translation as follows//Comparable is an interface implemented by all comparable types//(Boolean, number, string, pointer, channel, array of similar types,//The fields are structures of comparable types).//Comparable interfaces can only be used as type parameter constraints.// Not a type as a variable.type comparable interface{ comparable }
We can declare a type constraint ourselves.
type Number interface { int | int32 | int64 | float32 }
As defined above, if the parameter type is to beint
、int32
、int64
、float32
One of these four types is suitable and can be replaced by Number.
So we can modify and simplify the initially sized code.
package main import "fmt" type Number interface { int | int32 | int64 | float32 } func MaxNumber[K Number](a K, b K) K { if a > b { return a } else { return b } func main() { var a, b int var c, d float32 a = 1 b = 2 c = 1.1 d = 1.4 (MaxNumber(a, b)) (MaxNumber(c, d))
Use type constraints after the function name in brackets, then in the functionK
It means the type constraint Number, which can then be used in the function parameter list, function body, and return value.
It is necessary to addint
orfloat32
When such basic types are based on types, for exampletype MyInt int
, need to be added before the type name~
type Number interface { ~int }
If not added, the following error message will appear when using it
# generics-demo
.\:23:23: MyInt does not implement Number (possibly missing ~ for int in constraint Number)
At this point, all the generics of Go1.18 have appeared. Why don’t you try it? That’s all for the article. For more relevant Go1.18 generic content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!