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
+12.9k CodeIgniter : "Fatal error: Cannot use object of type stdClass as array" message
+12.9k Golang : How to get a user home directory path?
+6.7k Golang : Calculate BMI and risk category
+9.2k Golang : Accessing content anonymously with Tor
+21.5k Golang : How to reverse slice or array elements order
+18.6k Golang : Padding data for encryption and un-padding data for decryption
+20.2k Nginx + FastCGI + Go Setup.
+29k Golang : Save map/struct to JSON or XML file
+4.1k Javascript : How to show different content with noscript?
+5.8k Javascript : Get operating system and browser information
+5.3k Clean up Visual Studio For Mac installation failed disk full problem
+8k Golang : Check if integer is power of four example