golang contains

Starting with Go 1.18, you can use the slices package – specifically the generic Contains function: https://pkg.go.dev/golang.org/x/exp/slices#Contains.

go get golang.org/x/exp/slices
import  "golang.org/x/exp/slices"
things := []string{"foo", "bar", "baz"}
slices.Contains(things, "foo") // true

Note that since this is outside the stdlib as an experimental package, it is not bound to the Go 1 Compatibility Promise™ and may change before being formally added to the stdlib.

LEAVE A COMMENT