Go Program to Search for Array Items

In this Go program, we used for loop to iterate and search for array items and print the index position. Here, we used the If statement (if serArr[i] == search) to check whether any array item is equal to the search value. Once it is found, the flag value will be one (flag = 1), and the break will exit the GC from for loop. Next, we used the If else statement. If flag equals one (if flag == 1), then we found the search item in an array. Otherwise, we haven’t found it.

package main

import "fmt"

func main() {
    var sersize, i, search int

    fmt.Print("Enter the Even Array Size = ")
    fmt.Scan(&sersize)

    serArr := make([]int, sersize)

    fmt.Print("Enter the Even Array Items  = ")
    for i = 0; i < sersize; i++ {
        fmt.Scan(&serArr[i])
    }
    fmt.Print("Enter the Array Search Item  = ")
    fmt.Scan(&search)
    flag := 0
    for i = 0; i < sersize; i++ {
        if serArr[i] == search {
            flag = 1
            break
        }
    }
    if flag == 1 {
        fmt.Println("We Found the Search Item ", search, " at position = ", i)
    } else {
        fmt.Println("We haven't Found the Search Item ")
    }
}
Enter the Even Array Size = 3
Enter the Even Array Items  = 10 20 30
Enter the Array Search Item  = 20
We Found the Search Item  20  at position =  1

Go Program to Search the Array for an Item using For loop range

In this example, we used the println statement within the loop. It works to display the search items. However, this program doesn’t display anything if it does not find what we are looking for.

package main

import "fmt"

func main() {
    var sersize, i, search int

    fmt.Print("Enter the Even Array Size = ")
    fmt.Scan(&sersize)

    serArr := make([]int, sersize)

    fmt.Print("Enter the Even Array Items  = ")
    for i = 0; i < sersize; i++ {
        fmt.Scan(&serArr[i])
    }
    fmt.Print("Enter the Array Search Item  = ")
    fmt.Scan(&search)

    for k, sr := range serArr {
        if sr == search {
            fmt.Println("We Found the Search Item ", search, " at position = ", k)
            break
        }
    }
}
Enter the Even Array Size = 5
Enter the Even Array Items  = 20 30 40 20 50
Enter the Array Search Item  = 20
We Found the Search Item  20  at position =  0

In this Golang Program to Search for Array Items, we placed the if-else statement outside the loop to display the not found result.

package main

import "fmt"

func main() {
    var sersize, i, search, pos int

    fmt.Print("Enter the Even Array Size = ")
    fmt.Scan(&sersize)

    serArr := make([]int, sersize)

    fmt.Print("Enter the Even Array Items  = ")
    for i = 0; i < sersize; i++ {
        fmt.Scan(&serArr[i])
    }
    fmt.Print("Enter the Array Search Item  = ")
    fmt.Scan(&search)
    flag := 0
    for k, sr := range serArr {
        if sr == search {
            flag = 1
            pos = k
            break
        }
    }
    if flag == 1 {
        fmt.Println("We Found the Search Item ", search, " at position = ", pos)
    } else {
        fmt.Println("We haven't Found the Search Item ")
    }
}
Golang Program to Search for Array Elements 3