How to check if a slice in Go contains a specific element? (Contains for slice)
Hello everyone! I just started learning Go, coming from Python, and I encountered an unexpected problem.
In Python, I'm used to simply writing `if element in my_list:` to check if an element is in the list. In Go, I expected to find a similar method for slices, for example, `slice.Contains(element)`, but I didn't find anything like that.
I need to write a function that checks whether a specific string exists in a slice of strings. Here's an example of my code:
```go
package main
import "fmt"
func main() {
fruits := []string{"apple", "banana", "orange"}
search := "banana"
// How do I check if "banana" is in fruits?
// In Python I would write: if search in fruits:
// In Go I'm trying to find something similar...
fmt.Println(fruits) // just printing the slice
No answers yet.
Log in to leave an answer