Find the lowest number in GO / Find the biggest number in GO

Alessandro Giovanardi
2 min readOct 20, 2019

--

Today I am going to demonstrate how to find the lowest and the highest number from an ordered list of elements using GO programming language.

Find the lowest number in GO

First of all we create slice with an underlying array of numbers, in our case ints:

y := []int{34, 7, 456, 45, 99, 55, 33, 65, 234, 5, 66, 73, 64}

To find the lowest number in this array we first need to assume an index position as the min variable:

//assume lowest number is first index position 0
min := y[0]

After initializing variable min to index position 0 we can compare the value of that position against the following values we can find in the array sequence.

To accomplish with this task my suggestion is to use a range-based for loop to iterate over the values of the slice, then if the min value is higher than the compared value of the next position, a new value to min is set equal to the value which was the lowest in the comparison:

//if min is greater than v set min to v
for _, v := range y {
if min < v {
min = v
}
}

We can now print out the lowest number in the list with the command:

fmt.Println(“The lowest number is:”, min)

Enjoy!

The complete code is as it follows:

Find the biggest number in GO

On the other side the same chunk of code can be useful to understand how to retrieve the highest number in the list.

We could achieve this changing just a character:

1)

Change the “<” operator at line 15 to “>” : if min > v

That’s it!

If you want to support my work feel free to send a tip:

$BTC: 1EtMQNXADe5ZoSgbwox7Ug5TPvP8YWSpL3

$QRL: Q010400e08175fe9823e6caa1aa3359686d7251a40eb432230d55f5b4e7388ec1c947b4361fb5cf

--

--

Responses (1)