How to check if a value is present in an array golang?

Hi 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.

check,element,golang

Try it yourself

All the best 👍

Premium Content

You can get all the below premium content directly in your mail when you subscribe us

Books

Portfolio Template

View | Get Source Code

Cheat Sheets

Cheat Sheets Books are basically Important useful notes which we use in our day-to-day life.

I'm working on some more Cheat Sheets book on Jquery, TypeScript, React js and for other languages. I will send it once it's completed.

Stay tuned working on React Js Cheat Sheets Book

Related Posts