How to check if a value is present in an array golang?
May 18, 2022Hi Friends 👋,
Welcome To aGuideHub! ❤️
Today, we will learn to check the value available or not in a golang array, here we will create our custom function to check whether the value is present or not.
Here, we will create a sample array and store some value in it, after that we will call our function to check whether the value contains or not.
In our custom function which we are using to value is present in an array, we are using for loop and comparing values one by one to verify value available or not.
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 👍