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
+9k Golang : Channels and buffered channels examples
+5.6k Nginx : Password protect a directory/folder
+9.2k Golang : Convert file content to Hex
+5.9k Golang : Pat multiplexer routing example
+15k Golang : Find out mime type from bytes in buffer
+20.7k Golang : How to reverse slice or array elements order
+25.1k Golang : Convert file content into array of bytes
+4.9k Unix/Linux : How to open tar.gz file ?
+13.6k Golang : Find commonalities in two slices or arrays example
+21.2k Generate checksum for a file in Go
+19.9k Golang : Get password from console input without echo or masked
+31.3k Golang : Regular Expression for alphanumeric and underscore