Generics
Generic Functions
You can set a constraint, for example we have set comparable which means this only works on comparable types
func Contains[T comparable](a T, b []T) bool {
for _, ele := range b {
if a == ele {
return true
}
}
return false
}
func main() {
a := "Hail Mary"
b := []string{"Hail Mary", "Interstellar", "The Martian"}
println(Contains(a, b))
}Generic Types
type Val[T any] struct {
XValue T
}
func main() {
instance := Val[string]{XValue: "hello"}
println(instance.XValue)
}