How to check slice contains element in golang?
Hi Friends 👋,
Welcome To aGuideHub! ❤️
Today, we will learn to check slice contains an element in golang, here we will write a custom function to check elements exist or not.
So, first, we will create the isContains
function where we will iterate slice and compare every element.
Second, we will see how we can use our isContains
custom function in code, where we will pass some parameters like slice, and value.
Let’s dive into code.
package main
import "fmt"
func isContains(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}
return false
}
func main() {
s := []string{"infinitbility", "aguidehub", "notebility", "repairbility"}
fmt.Println(isContains(s, "aguidehub")) // true
fmt.Println(isContains(s, "notAvailable")) // false
}
When you run it you will so availability on boolean data true
or false
.

All the best 👍