Answers for "golang check if inside slice"

Go
0

golang check if inside slice

func contains(s []string, str string) bool {
	for _, v := range s {
		if v == str {
			return true
		}
	}

	return false
}

func main() {
	s := []string{"James", "Wagner", "Christene", "Mike"}
	fmt.Println(contains(s, "James")) // true
	fmt.Println(contains(s, "Jack")) // false
}
Posted by: Guest on June-28-2021

Browse Popular Code Answers by Language