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
+7.1k Golang : Find the shortest line of text example
+16.4k Golang : How to extract links from web page ?
+11.9k Golang : Verify Linux user password again before executing a program example
+14.6k Golang : How to determine if user agent is a mobile device example
+16k Golang : Get file permission
+16.5k Golang : How to implement two-factor authentication?
+11.7k Golang : Display a text file line by line with line number example
+10.7k Golang : Get local time and equivalent time in different time zone
+10.7k Golang : How to unmarshal JSON inner/nested value and assign to specific struct?
+15.7k Golang : Convert date format and separator yyyy-mm-dd to dd-mm-yyyy
+9.1k Golang : What is the default port number for connecting to MySQL/MariaDB database ?