How to Check if a Slice Contains a Value in Go with slices.Contains
slices.Contains(s, v) reports whether the value v is in the slice s: for a slice holding "customer" and "beta-tester", slices.Contains(roles, "customer") is true. The match is exact, so "Customer" isn't found, and the value must have the type of the slice's elements.
Syntax
<found> := slices.Contains(<s>, <v>)
| Part | What it does |
|---|---|
<s> | The slice to search, such as a []string. |
<v> | The value to look for. It must have the type of the slice's elements. |
<found> | true when an element equals <v>, otherwise false. |
The slices package documentation: "Contains reports whether v is present in s." Its signature, func Contains[S ~[]E, E comparable](s S, v E) bool, says that v has the slice's element type E, and that E must be comparable: a type whose values can be compared with ==.
The Example
The example looks for three roles in a user's slice of roles: customer, which is there, admin, which isn't, and Customer, which differs only in its capital letter. A changed version then looks for the text "500" in a slice of numbers.
Prerequisites
- Go 1.21 or later, which added the slices package.
go runcompiles and runs a single file. This guide uses Go 1.27.4.
Step 1: Write the Code
Create a file named main.go:

package main
import (
"fmt"
"slices"
)
func main() {
roles := []string{"customer", "beta-tester"}
fmt.Println(slices.Contains(roles, "customer"))
fmt.Println(slices.Contains(roles, "admin"))
fmt.Println(slices.Contains(roles, "Customer"))
}
Step 2: Run It
go run main.go
true
false
false
The command is the same in zsh, bash and PowerShell.
Step 3: Look for a Value of Another Type
Replace main.go with a version that looks for the string "500" in a slice of int values:
package main
import (
"fmt"
"slices"
)
func main() {
limits := []int{10, 500}
fmt.Println(slices.Contains(limits, "500"))
}
go run main.go
# command-line-arguments
./main.go:11:38: cannot use "500" (untyped string constant) as int value in argument to slices.Contains

What Happened
"customer"is in the slice, so the first call printedtrue."admin"isn't, so the second printedfalse."Customer"printedfalsetoo.slices.Containscompares with==, and two strings are equal only when every character matches, capital letters included.- The changed file didn't compile.
limitsis a[]int, so the value must be anint, and"500"is a string. The error came from the compiler before the program ran:# command-line-argumentsis the name thegocommand gives a program made of the files on its command line, and./main.go:11:38is the file, line and column.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Looking for text inside a string, as in slices.Contains("not-a-customer", "customer") | It doesn't compile: in call to slices.Contains, S (type string) does not satisfy ~[]E, because a string isn't a slice | Use strings.Contains, which finds text inside a string |
| Expecting a case-insensitive match | "Customer" isn't found in a slice holding "customer" | Use slices.ContainsFunc with strings.EqualFold, as in the FAQ below |
Looking up a number of another type in a []any, as in any(int64(500)) in []any{500} | false, because an int and an int64 are never equal, even when they hold the same number | Look the value up with the type the slice holds, here any(500), which printed true |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
cannot use "500" (untyped string constant) as int value in argument to slices.Contains | The value's type isn't the slice's element type | Pass a value of the element type, such as 500 |
in call to slices.Contains, S (type string) does not satisfy ~[]E | The first argument is a string, not a slice | Use strings.Contains |
undefined: slices | The slices package isn't imported | Add "slices" to the imports |
FAQ
How do I find where the value is?
Use slices.Index(s, v), which "returns the index of the first occurrence of v in s, or -1 if not present". For the example's roles, slices.Index(roles, "beta-tester") printed 1, and slices.Index(roles, "admin") printed -1.
How do I match without caring about capital letters?
slices.ContainsFunc takes a function instead of a value: it "reports whether at least one element e of s satisfies f(e)". strings.EqualFold reports whether two strings are equal when case is ignored, so slices.ContainsFunc(roles, func(r string) bool { return strings.EqualFold(r, "Customer") }) printed true.
What does slices.Contains return for an empty or nil slice?
false. For var roles []string, a nil slice with no elements, slices.Contains(roles, "customer") printed false, with no error.
Conclusion
slices.Contains(s, v) tells you whether a slice holds a value, with an exact, case-sensitive match and a value of the slice's element type. Use slices.Index when you need the position, and strings.Contains for text inside a string.
Additional Resources
- slices.Contains, slices.ContainsFunc and slices.Index, in the Go standard library documentation.
- strings.Contains and strings.EqualFold.
