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
+20.2k SSL : How to check if current certificate is sha1 or sha2
+5.9k Golang : Levenshtein distance example
+8.6k Golang : Validate IPv6 example
+5.8k Golang : Fixing Gorilla mux http.FileServer() 404 problem
+5k Golang : Compound interest over time example
+12.1k Swift : Convert (cast) Int to String ?
+18.1k Golang : How to run your code only once with sync.Once object
+13.5k Golang : Save(pipe) HTTP response into a file
+14.2k Golang : Intercept Ctrl-C interrupt or kill signal and determine the signal type
+28.3k Golang : Get and Set User-Agent examples
+4.3k Golang *File points to a file or directory ?
+6.9k Golang : Gomobile init produce "iphoneos" cannot be located error