In this examples, You will learn how to find the minimum/smallest element of an array or slice from input from the user in Go Language
What are the benefits of using Go compared to other languages? Unlike other languages which.
The conditional for-loop in GoLang Simply excluding the initialization and postcondition, we can create another kind of for-loop which is the conditional for-loop. It only checks the condition and runs if it matches. Different ways to send an email with Golang. In this blog, we’ll look at different methods to send an email with Go, First we will explore inbuilt smtp package, then we will move to use a popular package Gomail and finally we will send HTML emails using custom. The reason why GoLang provides the max/min function for comparing float64 numbers are that floating numbers are very complicated to handle for most of developers, it's not that straightforward to compare two float numbers given the nature of computer architecture.
To understanding the below programs, You have following features understanding 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 traversed.
Example Program Minimum/smaller number of an array of a slice of numbers
This program explains about 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 are completed
Output is
Example program to check a minimum of array/slice elements read user input from the console
Instead of array elements are fixed like above, This program read the array size and elements read from input console by a user using Scanln function
The logic is same as the above program to calculate smallest of an array or slice of numbers
The output of the above code is as follows
time package in golang defines two ways of adding or subtracting to a time.
- Add function – It is used to add/subtract a duration to time t. Since duration can be represented in hours, minutes, seconds, milliseconds, microseconds and nanoseconds, therefore Add function can be used to add/subtract hours, minutes, seconds, milliseconds, microseconds and nanoseconds from a time. Its signature is
- AddDate function – It is used to add/subtract years, months and days to time t. Its signature is
Note: Positive values are used to add to time and negative values are used to subtract. Let’s see a working example of Add and Subtract to time.
Below code can be used to add to time
Output
Golang Mind Map
Below code can be used to subtract to time
Golang Minio
Output: