{

# Golang Example-Program to find Minimum of array or slice In these examples, You will learn how to find the minimum/smallest element of an `array` or `slice` from input from the user in Go Language.

To understand the below programs, You have the understanding of the following features in Go Language.

The algorithm for this program is as follows.

At first, the starting element of an array assumed as the smallest element will compare with other elements. if elements are smaller than other elements, It will be a new smaller element, This sequence of steps continues execution until all elements are iterated.

## Golang example to find a minimum number in array or slice

This program explains the following sequence of steps.

• `Array` or `slice` is declared and initialized with values
• Declared smallestNumber initialized with array first element assumed as the smallest number
• Iterated `array` or `slice` using for loop with range to calculate minimum element
• Inside for loop for each element, if an element is smallestNumber, assign smallestNumber with an element
• repeat loop until all elements iteration completed.
``````package main
import "fmt"

func main() {

array := []int{11, 9, 1, 45, 411}
smallestNumber := array
for _, element := range array {
if element < smallestNumber {
smallestNumber = element

}
}
fmt.Println("Smallest number of Array/slice is  ", smallestNumber)

}

``````

Output:

``````Smallest number of Array/slice is   1

``````

## Example program to check a minimum of array/slice elements read user input from the console

Instead of array elements being fixed like above, This program read the array size and elements read from the input console by a user using the Scanln function.

The logic is the same as the above program to calculate the smallest of an array or slice of numbers.

``````package main

import "fmt"

func main() {
size := 0
fmt.Print("Number of elements n=")
fmt.Scanln(&size)
fmt.Println("Enter of array elements")
elements := make([]int, size)
for i := 0; i < size; i++ {
fmt.Scanln(&elements[i])
}
fmt.Println("Entered Array of elements:", elements)
smallestNumber := elements
for _, element := range elements {
if element < smallestNumber {
smallestNumber = element

}
}
fmt.Println("Smallest number of Array/slice is  ", smallestNumber)

}

``````

The output of the above code is as follows.

``````Number of elements n=5
Enter of array elements
2
5
0
1
8
Entered Array of elements: [2 5 0 1 8]
Smallest number of Array/slice is   0

``````
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.

Related posts