package main
import "fmt"
func linearSearch(arr []int, target int) int {
for i, val := range arr {
if val == target {
return i // found target, return index
}
}
return -1 // target not found
}
func main() {
arr := []int{3, 7, 1, 9, 5}
target := 9
index := linearSearch(arr, target)
if index != -1 {
fmt.Printf("Index found: %d\n", index)
} else {
fmt.Println("Value not found")
}
}for i, val := range arr {
iterate over each element to check for target
compare current element with target value
return i // found target, return index
stop search and return index when target found
return -1 // target not found
return -1 if target is not in array