- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to check if a slice contains an element in Golang?
Many languages do provide a method similar to indexOf() where one can find the existence of a particular element in an array-like data structure. However, in Golang, there's no such method and we can simply implement it with the help of a for-range loop.
Let's suppose we have a slice of strings, and we want to find out whether a particular string exists in the slice or not.
Example 1
Consider the code shown below.
package main import ( "fmt" ) func Contains(sl []string, name string) bool { for _, value := range sl { if value == name { return true } } return false } func main() { sl := []string{"India", "Japan", "USA", "France"} countryToCheck := "Argentina" isPresent := Contains(sl, countryToCheck) if isPresent { fmt.Println(countryToCheck, "is present in the slice named sl.") } else { fmt.Println(countryToCheck, "is not present in the slice named sl.") } }
In the above code, we are trying to find if the string with the value " Argentina" is present in the slice "sl" or not.
Output
If we run the command go run main.go, then we will get the following output in the terminal.
Argentina is not present in the slice named sl.
We can also print the index at which we have encountered an element in a slice.
Example 2
Consider the code shown below.
package main import ( "fmt" ) func Contains(sl []string, name string) int { for idx, v := range sl { if v == name { return idx } } return -1 } func main() { sl := []string{"India", "Japan", "USA", "France"} countryToCheck := "USA" index := Contains(sl, countryToCheck) if index != -1 { fmt.Println(countryToCheck, "is present in the slice named sl at index", index) } else { fmt.Println(countryToCheck, "is not present in the slice named sl.") } }
Output
If we run the command go run main.go then we will get the following output in the terminal.
USA is present in the slice named sl at index 2
- Related Articles
- Golang to check if a slice is empty
- Check if a Stack contains an element in C#
- How to check if a string contains a substring in Golang?
- Golang program to remove an element from a slice
- Golang Program to Check if An Array Contains a Given Value
- How to check if Java list contains an element or not?
- Golang program to search an element in the slice
- Check if the specified element is present in the slice of bytes in Golang
- Check if the given slice is sorted in Golang
- Golang program to check if a string contains a substring
- How to replace a specified element in slice of bytes in Golang?
- Golang program to check if a set is the subset of another slice
- Golang program to find the largest element in a slice
- How to check if ArrayList contains an item in Java?
- Check if a HashSet contains the specified element in C#
