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
+15.3k Golang : Convert date format and separator yyyy-mm-dd to dd-mm-yyyy
+11.5k Golang : Setup API server or gateway with Caddy and http.ListenAndServe() function example
+10.2k Golang : Create matrix with Gonum Matrix package example
+7.5k Golang : Getting Echo framework StartAutoTLS to work
+5.3k PHP : Convert CSV to JSON with YQL example
+19.5k Golang : Accept input from user with fmt.Scanf skipped white spaces and how to fix it
+9.8k Golang : Bcrypting password
+6.4k Golang : Check if password length meet the requirement
+16.7k Golang : Get number of CPU cores
+15.6k Golang : Read a file line by line
+7.9k Golang : HttpRouter multiplexer routing example
+5.2k Golang : Stop goroutine without channel