Golang : Integer is between a range
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
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
+2.8k JavaScript/JQuery : Redirect page examples
+6.4k Golang : Go as a script or running go with shebang/hashbang style
+17k Golang : How to get time zone and load different time zone?
+4.4k Golang : How to find out similarity between two strings with Jaro-Winkler Distance?
+5.3k Golang : Capture text return from exec function example
+12.2k Golang : Iterating Elements Over A List
+3.3k Unix/Linux/MacOSx : Get local IP address
+18.3k Golang : Securing password with salt
+10.9k Golang : Parsing or breaking down URL
+3.8k Golang : Missing Subversion command
+4.1k WARNING: UNPROTECTED PRIVATE KEY FILE! error message
+11.7k Golang : How to tell if a file is compressed either gzip or zip ?