Golang : Check if an integer is negative or positive
A simple example on how to check if a given number is negative or positive number in Golang.
Here you go!
package main
import (
"fmt"
"math"
)
func main() {
integer := -1.0
fmt.Println("Integer is negative number :", math.Signbit(integer))
integer2 := 1.0
fmt.Println("Integer2 is negative number : ", math.Signbit(integer2))
}
Output:
Integer is negative number : true
Integer2 is negative number : false
See also : Golang : Count number of digits from given integer value
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
+20k Golang : Measure http.Get() execution time
+40.7k Golang : Convert to io.ReadSeeker type
+8.3k How to show different content from website server when AdBlock is detected?
+10.3k Golang : Random Rune generator
+14.8k Golang : GUI with Qt and OpenCV to capture image from camera
+19k Golang : How to make function callback or pass value from function as parameter?
+10.5k Golang : Generate random integer or float number
+5.1k Golang : Constant and variable names in native language
+21.3k Golang : Convert(cast) string to rune and back to string example
+8k Golang : How to feed or take banana with Gorilla Web Toolkit Session package
+10.4k Golang : Embed secret text string into binary(executable) file
+11.8k Golang : Display a text file line by line with line number example