How to Check If a Slice Contains a Value in Go

Use slices.Contains to check if a value exists in a Go slice.

Use the slices.Contains function from the slices package to check if a slice contains a specific value. This function returns true if the value exists in the slice, and false otherwise.

package main

import (
	"fmt"
	"slices"
)

func main() {
	nums := []int{1, 2, 3, 4, 5}
	if slices.Contains(nums, 3) {
		fmt.Println("Found 3")
	}
}