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
+22.5k Golang : Strings to lowercase and uppercase example
+8.4k Linux/Unix : fatal: the Postfix mail system is already running
+9.1k Golang : How to check if a string with spaces in between is numeric?
+9.3k Golang : Detect Pascal, Kebab, Screaming Snake and Camel cases
+8.8k Golang : GMail API create and send draft with simple upload attachment example
+28.4k Get file path of temporary file in Go
+17.4k Golang : Clone with pointer and modify value
+13.9k Golang: Pad right or print ending(suffix) zero or spaces in fmt.Printf example
+7.8k Setting $GOPATH environment variable for Unix/Linux and Windows
+18.4k Golang : Send email with attachment
+13.4k Golang : How to get year, month and day?