Skip to content

Instantly share code, notes, and snippets.

@montanaflynn
Created June 19, 2020 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save montanaflynn/6f91a58d1538cb23db959a111389b58d to your computer and use it in GitHub Desktop.
Save montanaflynn/6f91a58d1538cb23db959a111389b58d to your computer and use it in GitHub Desktop.
Golang generic Contains function https://blog.golang.org/generics-next-step
package main
import (
"fmt"
)
func Contains(type T comparable)(elems []T, target T) bool {
for _, elem := range elems {
if elem == target {
return true
}
}
return false
}
func main() {
fmt.Println(Contains([]int{1, 2, 3, 4, 5}, 4))
fmt.Println(Contains([]string{"a", "b", "c"}, "a"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment