Golang : Integer is between a range
Tags : golang minmax in-between-range integer
Problem:
You need to check if a given integer is in between a range of integer. How to do that?
Solution:
Check if the given integer is more than a minimum and less than a maximum.
Here you go!
package main
import (
"fmt"
)
func InBetween(i, min, max int) bool {
if (i >= min) && (i <= max) {
return true
} else {
return false
}
}
func main() {
fmt.Println("Is 2 between 1 and 3 : ", InBetween(2, 1, 3))
fmt.Println("Is 2 between 5 and 99 : ", InBetween(2, 5, 99))
}
Output:
Is 2 between 1 and 3 : true
Is 2 between 5 and 99 : false
Reference:
https://www.socketloop.com/tutorials/golang-how-to-check-if-ip-address-is-in-range
See also : Golang : How to check if IP address is in range
Tags : golang minmax in-between-range integer
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+3.3k Golang : get the current working directory of a running program
+4.8k Golang : Wait and sync.WaitGroup example
+6.2k Golang : Get number of CPU cores
+2.3k WARNING: UNPROTECTED PRIVATE KEY FILE! error message
+3.8k Swift : Convert (cast) String to Float
+3.9k Golang : Convert file unix timestamp to UTC time example
+8.2k Golang : Print leading(padding) zero or spaces in fmt.Printf?
+2.1k Golang : Generate human readable password
+7.4k Golang : Find smallest number in array
+10.9k Golang : Pipe output from one os.Exec(shell command) to another command
+2.2k Android Studio : Import third-party library or package into Gradle Scripts