1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
| package main
import (
"fmt"
)
func main() {
items := []string{"A", "1", "B", "2", "C", "3"}
// Missing Example
_, found := Find(items, "golangcode.com")
if !found {
fmt.Println("Value not found in slice")
}
// Found example
k, found := Find(items, "B")
if !found {
fmt.Println("Value not found in slice")
}
fmt.Printf("B found at key: %d\n", k)
}
// Find takes a slice and looks for an element in it. If found it will
// return it's key, otherwise it will return -1 and a bool of false.
func Find(slice []string, val string) (int, bool) {
for i, item := range slice {
if item == val {
return i, true
}
}
return -1, false
}
|