Go Program to Search for an Element in an Array

1. Introduction

Arrays, the sequential collection of elements, are fundamental structures in computer science. Often, there arises a need to determine if a particular element exists within an array. This operation, known as searching, is one of the foundational algorithms. While Go provides ways to perform this with slices and maps, understanding the foundational search operation in a basic array can be invaluable. In this post, we will create a simple Go program to search for an element in an array.

2. Program Overview

The Go program will:

1. Define an array with a set of elements.

2. Take a number from the user for which a search will be executed within the array.

3. Perform the search operation.

4. Display the outcome of the search to the user.

3. Code Program

// We embark with the main package declaration.
package main

// The fmt package is beckoned for its input and output prowess.
import "fmt"

// The function to execute our search.
func searchElement(arr []int, element int) bool {
    // Iterate over each item in the array.
    for _, value := range arr {
        if value == element {
            return true // Element found!
        }
    }
    return false // If the loop completes without finding the element.
}

// The heart of our program, the main function.
func main() {
    array := []int{5, 2, 9, 1, 5, 6}
    var searchNum int

    // Seeking the desired number from the user.
    fmt.Print("Enter number to search in the array: ")
    fmt.Scan(&searchNum)

    // The search commences.
    if searchElement(array, searchNum) {
        fmt.Printf("%d is present in the array.\n", searchNum)
    } else {
        fmt.Printf("%d is not found in the array.\n", searchNum)
    }
}

Output:

For instance, if a user inputs 9, the program will pronounce:
9 is present in the array.

Conversely, an input of 3 will yield:
3 is not found in the array.

4. Step By Step Explanation

1. Package and Import: It begins by declaring the main package and imports the fmt package for input and output functionalities.

2. Search Function: The searchElement function:

  • Takes in an array of integers and an integer element as parameters.
  • It iteratively checks each value of the array. If a match is found (i.e., any array value equals the provided element), the function immediately returns true. 
  • If the function exits the loop without identifying the element, it denotes the element wasn't in the array and returns false.

3. Main Function: The main function perform the following: 

  • Initializes an integer array named array. 
  • Declares a variable searchNum to capture the number the user wishes to search for. 
  • It then queries the user for this number. 
  • Utilizing the searchElement function, it ascertains whether the entered number is present in the array or not. 
  • Finally, based on the function's outcome, the program informs the user if the number was located in the array or not.

Comments