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.2k Golang : md5 hash of a string
+7.4k Golang : How to convert strange string to JSON with json.MarshalIndent
+11.5k Golang : Format numbers to nearest thousands such as kilos millions billions and trillions
+7.9k Javascript : Put image into Chrome browser's console
+12.8k Golang : Add ASCII art to command line application launching process
+7.3k Golang : alternative to os.Exit() function
+6.7k Golang : Skip or discard items of non-interest when iterating example
+6.8k Unix/Linux : How to fix CentOS yum duplicate glibc or device-mapper-libs dependency error?
+16.5k Golang : Get IP addresses of a domain name
+19.6k Golang : Close channel after ticker stopped example
+5.6k PHP : Fix Call to undefined function curl_init() error
+13.1k Golang : How to get a user home directory path?