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
+3.8k Golang : Experimental Jawi programming language
+12.1k Golang : How to get a user home directory path?
+9.1k Golang : Get login name from environment and prompt for password
+8.2k Golang : How to control fmt or log print format?
+6.8k Javascript : How to check a browser's Do Not Track status?
+5.2k Golang : Detect face in uploaded photo like GPlus
+14.1k Golang : Get query string value on a POST request
+7.1k Golang : Variadic function arguments sanity check example
+19.8k Golang : For loop continue,break and range
+8.6k Javascript : Read/parse JSON data from HTTP response
+10.3k Golang : Generate DSA private, public key and PEM files example