Created
June 19, 2020 06:18
-
-
Save montanaflynn/6f91a58d1538cb23db959a111389b58d to your computer and use it in GitHub Desktop.
Golang generic Contains function https://blog.golang.org/generics-next-step
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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