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
+28.6k Golang : How to get HTTP request header information?
+13k Golang : Google Drive API upload and rename example
+13.6k Golang : Adding XML attributes to xml data or use attribute to differentiate a common tag name
+4.8k Golang : Markov chains to predict probability of next state example
+34.3k Golang : Validate IP address
+7.4k Python : Fix SyntaxError: Non-ASCII character in file, but no encoding declared
+23.3k Golang : GORM read from database example
+6.3k Golang : How to stop user from directly running an executable file?
+9.3k Golang : Allow Cross-Origin Resource Sharing request
+12.7k Golang : Fix image: unknown format error
+26.6k Golang : Decode/unmarshal unknown JSON data type with map[string]interface
+8.6k Golang : Validate IPv6 example