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
+12.5k Golang : Convert(cast) int to float example
+11.6k Golang : Get uploaded file name or access uploaded files
+7.8k Golang : Use regular expression to get all upper case or lower case characters example
+8.9k Golang : Simple file scaning and remove virus example
+14.6k Golang : How to generate QR codes?
+21.2k Find and replace a character in a string in Go
+12.2k Golang : Submit web forms without browser by http.PostForm example
+66.3k Golang : How to return HTTP status code?
+4.3k Golang : Measure execution time for a function
+10.7k Golang : Count number of runes in string
+4.6k Linux/Unix : Commands that you need to be careful about