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
+14.4k Golang : GUI with Qt and OpenCV to capture image from camera
+14.1k Golang : Chunk split or divide a string into smaller chunk example
+16.9k Golang : Get input from keyboard
+36k Golang : Save image to PNG, JPEG or GIF format.
+10.9k Golang : Create S3 bucket with official aws-sdk-go package
+4.6k Unix/Linux : How to pipe/save output of a command to file?
+19.8k Golang : Convert(cast) bytes.Buffer or bytes.NewBuffer type to io.Reader
+20k Golang : Count number of digits from given integer value
+5.9k Javascript : Get operating system and browser information
+12k Golang : Save webcamera frames to video file
+19.2k Golang : Fix cannot download, $GOPATH not set error
+8.8k Golang : Populate or initialize struct with values example