This is another golang data structure tutorial, We will Implement Linear Search using Golang.The linear search is use to search element into the items, This algorithm used to search element one by one from starting index, one found return true or if does not found then return false.
package main import "fmt" func main() { arr := []int {87,58,46,78,35,86,19,21,20} a := linearsearch(arr, 35) fmt.Printf("The Source Array : %v\n", arr) fmt.Printf("The element %v is found at %v location", 35, a) } func linearsearch(arr []int, search int) int { for i, item:= range arr { if(item == search) { return i+1 } } return 0 }
In the above golang code, We have passed constant array to and search 35, if the 35 number is found into the given array then return number index value otherwise return 0 index.
We have used golang range function to iterate on array, its very powerful method to iterate on arrays.
The post How to Implement Linear Search In Golang appeared first on Rest Api Example.