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
+11.5k Golang : Display a text file line by line with line number example
+15.5k Golang : rune literal not terminated error
+8.4k PHP : How to parse ElasticSearch JSON ?
+11.4k Use systeminfo to find out installed Windows Hotfix(s) or updates
+5.2k Javascript : Change page title to get viewer attention
+13.5k Golang : Strings comparison
+8.8k Golang : Find network service name from given port and protocol
+11.5k SSL : The certificate is not trusted because no issuer chain was provided
+24.4k Golang : Change file read or write permission example
+11.1k CodeIgniter : How to check if a session exist in PHP?
+16.3k Golang :Trim white spaces from a string
+5.7k Unix/Linux/MacOSx : Get local IP address