Comparing two generic values

Report a typo

Bruno has created a Go program with the generic function contains, which checks if a certain value exists in a slice. It returns true if the value exists and false otherwise.

However, when Bruno tried to run his program, he got the following compilation error:

invalid operation: e == val (type parameter T is not comparable with ==)

Please help Bruno fix the declaration of the generic function contains. Remember what data type we should use when we need to compare two generic values!

Sample Input 1:

you shall not pass!
1 2 3
shall 1

Sample Output 1:

true true
Write a program in Go
package main

import "fmt"

// What is the correct type parameter to use when you need to compare two values?
func contains[T any](slice []T, val T) bool {
for _, e := range slice {
if e == val {
return true
}
}
return false
}

// DO NOT delete or modify the contents of the main function!
func main() {
const (
slice1Len = 4
slice2Len = 3
)

slice1 := make([]string, slice1Len)
fmt.Scanln(&slice1[0], &slice1[1], &slice1[2], &slice1[3])

slice2 := make([]int, slice2Len)
fmt.Scanln(&slice2[0], &slice2[1], &slice2[2])

var val1 string
var val2 int
fmt.Scanln(&val1, &val2)

fmt.Println(contains(slice1, val1), contains(slice2, val2))
}
___

Create a free account to access the full topic