Type constraints
Context
Section titled “Context”Type constraints limit what types can be used as type arguments. The built‑in constraint any allows all types. comparable allows types that can be compared with == and !=. You can also define custom constraints using an interface.
Example
Section titled “Example”Use comparable to check if a slice contains a specific value.
Code example
Section titled “Code example”package main
import "fmt"
func Contains[T comparable](slice []T, value T) bool { for _, v := range slice { if v == value { return true } } return false}
func main() { ints := []int{1, 2, 3} fmt.Println(Contains(ints, 2)) // true fmt.Println(Contains(ints, 5)) // false
strs := []string{"apple", "banana"} fmt.Println(Contains(strs, "banana")) // true}Output
Section titled “Output”truefalsetrue