OneCompiler

How to check if a slice contains given string in Go

I need to check whether or not a given String present in a slice, how can I do that in golang?

1 Answer

4 years ago by

Golang by default does not have a util method to check this. Following method can help to do this

func SliceContainsItem(slice []string, item string) bool {
	for _, ele := range slice {
        if ele == item {
            return true
        }
    }
  return false
}

4 years ago by Karthik Divi