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.


Try it yourself

All the best 👍

Premium Content

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


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.

Related Posts