How to check if a slice contains a value in Go
In Go 1.21 or newer, use slices.Contains(values, target) to check whether a slice contains a value. It returns a boolean, stops at the first match, and returns false for an empty or nil slice. If you need to compare a field or ignore letter case, pass a predicate to slices.ContainsFunc. Go slices documentation
Does a slice contain this string?
package main
import (
"fmt"
"slices"
)
func main() {
roles := []string{"reader", "editor", "admin"}
fmt.Println(slices.Contains(roles, "editor")) // true
fmt.Println(slices.Contains(roles, "Editor")) // false
fmt.Println(slices.Index(roles, "admin")) // 2
}
Contains uses Go’s equality operator, so string comparisons are case-sensitive. It also works for integers and other comparable element types. slices.Index returns the position of the first match, or -1 if the target is absent. On Go versions before 1.21, a range loop with an early return gives the same membership check. Go 1.21 release notes · slices.Index
How do I match a field or ignore case?
A struct with a slice field cannot be compared with ==. You can still search a slice of those structs by deciding which field identifies a match.
package main
import (
"fmt"
"slices"
"strings"
)
type Team struct {
Name string
Members []string
}
func main() {
teams := []Team{{Name: "Platform"}, {Name: "Payments"}}
found := slices.ContainsFunc(teams, func(team Team) bool {
return strings.EqualFold(team.Name, "platform")
})
fmt.Println(found) // true
}
For a plain slice of strings, the same approach takes a func(s string) bool predicate. If you need every matching team, filter the slice. slices.ContainsFunc · strings.EqualFold
When should I build a set?
For a single lookup, start with the slice you already have. A miss scans the entire slice. If the same collection serves many exact-match queries, build a map once and reuse it across requests. Use the second result of the lookup to test membership.
package main
import "fmt"
func main() {
roles := []string{"reader", "editor", "admin"}
allowed := make(map[string]struct{}, len(roles))
for _, role := range roles {
allowed[role] = struct{}{}
}
for _, candidate := range []string{"admin", "guest"} {
_, ok := allowed[candidate]
fmt.Println(candidate, ok) // admin true, then guest false
}
}
The map costs memory and represents the roles at the time you built it. If roles change, update the map or rebuild it before relying on the answer. If the slice is already sorted, slices.BinarySearch gives you a membership result and an insertion index without building a second collection. Go map lookups · slices.BinarySearch · Implementing a set in Go
How much does building the map cost?
This benchmark searches distinct nine-character strings. It measures finding the first string and missing a string of the same length, with 8 and 256 items. The existing-set column reuses a map built before timing. The last column includes building it from the slice for each lookup.
| Strings | Query | Scan slice (ns/op) | Existing set (ns/op) | Build set and check (ns/op) |
|---|---|---|---|---|
| 8 | First item | 1.6 | 4.2 | 102.7 |
| 8 | Missing item | 10.7 | 5.8 | 104.4 |
| 256 | First item | 1.6 | 4.5 | 2398.0 |
| 256 | Missing item | 309.9 | 3.8 | 2395.0 |
These are median nanoseconds per operation from five runs on an Apple M4 with Go 1.26.2 on macOS/arm64. The first-hit case queries the string already held in the first slot. Real results depend on the keys, hit rate, match position and number of lookups per build. For an occasional lookup, building the map dominated the time in all four cases. For repeated misses against the 256-item collection, reusing the map saved a full scan each time.
Download the benchmark source as lookup_test.go and run this command with Go 1.24 or newer.
go test -bench=. -benchmem -benchtime=150ms -count=5 lookup_test.go