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
+6.8k Javascript : Put image into Chrome browser's console
+4.1k Linux/Unix/MacOSX : Find out which application is listening to port 80 or use which IP version
+13.6k Golang : Adding XML attributes to xml data or use attribute to differentiate a common tag name
+37.7k Golang : How to read CSV file
+7.9k Golang : How to extract video or image files from html source code
+3.5k Javascript : Detect when console is activated and do something about it
+18.3k Golang : Close channel after ticker stopped example
+4.5k Golang : fmt.Println prints out empty data from struct
+15k Golang : Execute terminal command to remote machine example
+7.8k Golang : Find duplicate files with filepath.Walk
+4.7k Golang : Configure crontab to poll every two minutes 8am to 6pm Monday to Friday
+8.7k Golang : Sort and reverse sort a slice of integers