Go Program to Find Smallest Array Item

Write a Go program to find the Smallest Array Item or number using For loop. First, we assigned the first array item to the smallest variable (smallest = smArr[0]). Within the for loop (for i = 0; i < smsize; i++), the if condition (if smallest > smArr[i]) checks whether the current array item is greater than the smallest. If True, assign that value to the smallest variable, and put the index value in the position variable.

package main

import "fmt"

func main() {
    var smsize, i, position int

    fmt.Print("Enter the Array Size to find the Smallest = ")
    fmt.Scan(&smsize)

    smArr := make([]int, smsize)

    fmt.Print("Enter the Smallest Array Items  = ")
    for i = 0; i < smsize; i++ {
        fmt.Scan(&smArr[i])
    }
    smallest := smArr[0]

    for i = 0; i < smsize; i++ {
        if smallest > smArr[i] {
            smallest = smArr[i]
            position = i
        }
    }
    fmt.Println("\nThe Smallest Number in this smArr    = ", smallest)
    fmt.Println("The Index Position of Smallest Number = ", position)
}
Enter the Array Size to find the Smallest = 5
Enter the Smallest Array Items  = 10 8 22 4 19

The Smallest Number in this smArr    =  4
The Index Position of Smallest Number =  3

Go Program to Find Smallest Array Number using For Loop Range

package main

import "fmt"

func main() {
    var smsize, i, position int

    fmt.Print("Enter the Array Size to find the Smallest = ")
    fmt.Scan(&smsize)

    smArr := make([]int, smsize)

    fmt.Print("Enter the Smallest Array Items  = ")
    for i = 0; i < smsize; i++ {
        fmt.Scan(&smArr[i])
    }
    smallest := smArr[0]

    for i, sm := range smArr {
        if smallest > sm {
            smallest = sm
            position = i
        }
    }
    fmt.Println("\nThe Smallest Number in this smArr    = ", smallest)
    fmt.Println("The Index Position of Smallest Number = ", position)
}
Go Program to Find Smallest Array Item 2

In this Golang program, we created a function (smallestNum(smArr []int) (int, int)) that will return the array smallest item or number and index position.

package main

import "fmt"

var smallest, position int

func smallestNum(smArr []int) (int, int) {
    smallest = smArr[0]
    for i, sm := range smArr {
        if smallest > sm {
            smallest = sm
            position = i
        }
    }
    return smallest, position
}

func main() {
    var smsize, i int

    fmt.Print("Enter the Array Size to find the Smallest = ")
    fmt.Scan(&smsize)

    smArr := make([]int, smsize)

    fmt.Print("Enter the Smallest Array Items  = ")
    for i = 0; i < smsize; i++ {
        fmt.Scan(&smArr[i])
    }
    smallest, position := smallestNum(smArr)

    fmt.Println("\nThe Smallest Number in this smArr     = ", smallest)
    fmt.Println("The Index Position of Smallest Number = ", position)
}
Enter the Array Size to find the Smallest = 6
Enter the Smallest Array Items  = 9 129 33 1 8 60

The Smallest Number in this smArr     =  1
The Index Position of Smallest Number =  3