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
+7k Golang : How to iterate a slice without using for loop?
+23k Golang : Read a file into an array or slice example
+10.8k Golang : Fix go.exe is not compatible with the version of Windows you're running
+5.6k Golang : Find change in a combination of coins example
+9.8k CodeIgniter : Load different view for mobile devices
+7.3k Golang : Gorrila set route name and get the current route name
+7.1k Golang : alternative to os.Exit() function
+36.1k Golang : Convert date or time stamp from string to time.Time type
+9.9k Golang : Edge detection with Sobel method
+5.2k Golang : Reclaim memory occupied by make() example
+7.6k Setting $GOPATH environment variable for Unix/Linux and Windows
+30k Golang : How to verify uploaded file is image or allowed file types