Skip to main content

How to Check if a Slice Contains a Value in Go with slices.Contains

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
<found> := slices.Contains(<s>, <v>)
PartWhat 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​

Step 1: Write the Code​

Create a file named main.go:

main.go, which imports fmt and slices, sets roles to a slice holding customer and beta-tester, and prints slices.Contains for customer, admin and Customer.

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​

Terminal
go run main.go
Output
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:

main.go
package main

import (
"fmt"
"slices"
)

func main() {
limits := []int{10, 500}

fmt.Println(slices.Contains(limits, "500"))
}
Terminal
go run main.go
Output
# command-line-arguments
./main.go:11:38: cannot use "500" (untyped string constant) as int value in argument to slices.Contains

A zsh terminal where go run main.go prints true, false and false, then, for the changed file, stops with the compile error 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 printed true.
  • "admin" isn't, so the second printed false.
  • "Customer" printed false too. slices.Contains compares with ==, and two strings are equal only when every character matches, capital letters included.
  • The changed file didn't compile. limits is a []int, so the value must be an int, and "500" is a string. The error came from the compiler before the program ran: # command-line-arguments is the name the go command gives a program made of the files on its command line, and ./main.go:11:38 is the file, line and column.

Common Mistakes​

MistakeWhat happensDo 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 sliceUse 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 numberLook the value up with the type the slice holds, here any(500), which printed true

Troubleshooting​

SymptomLikely causeFix
cannot use "500" (untyped string constant) as int value in argument to slices.ContainsThe value's type isn't the slice's element typePass a value of the element type, such as 500
in call to slices.Contains, S (type string) does not satisfy ~[]EThe first argument is a string, not a sliceUse strings.Contains
undefined: slicesThe slices package isn't importedAdd "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​