Junior — Middle
How can it be determined whether a specific element is present in a list (slice)?
sobes.tech AI
Answer from AI
In Go, to determine whether an element exists in a slice, it is common to iterate through the slice and check each element. For example, for a slice of strings:
func contains(slice []string, item string) bool {
for _, v := range slice {
if v == item {
return true
}
}
return false
}
This approach is universal for any types for which comparison is defined. If you need to check for the presence of elements frequently, you can use a map for more efficient searching.