resource:
https://www.youtube.com/watch?v=Pa_e9EeCdy8
https://go.dev/blog/intro-generics
constrains: https://pkg.go.dev/golang.org/x/exp/constraints
why do we want generics when we can use interface{}
using interface{} , during the compilation we have no idea of what type of data we are dealing with, and it is more bug prone, and can cause runtime errors
if we want to write a function which takes in the arguments of same data type, it is not possible with the interface{}
we can fix this by generics
syntax to use generics in go:
The basic syntax for using generics in Go involves defining type parameters and using them within functions, structs, and interfaces. Here's a breakdown of the key elements:
Type parameters are declared using the [] brackets followed by the identifier and keyword any. This indicates that the type parameter can be any valid Go type. For example, to define a function that takes two values of the same type and returns their sum, you would use:
func Sum[T any](x, y T) T {
return x + y
}