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
+7.5k Golang : Gorrila set route name and get the current route name
+13k Python : Convert IPv6 address to decimal and back to IPv6
+4.7k MariaDB/MySQL : How to get version information
+10.7k Golang : Bubble sort example
+10.2k Golang : Identifying Golang HTTP client request
+8.1k Golang : Sort words with first uppercase letter
+29.5k Golang : JQuery AJAX post data to server and send data back to client example
+20.5k Golang : Check if os.Stdin input data is piped or from terminal
+5.5k Golang : Display advertisement images or strings on random order
+8.9k Golang : Executing and evaluating nested loop in html template
+46.7k Golang : Marshal and unmarshal json.RawMessage struct example
+27.5k Golang : Convert CSV data to JSON format and save to file