take the absolute value of a number in go

In Go, you can get the absolute value of a number by using the math.Abs() function from the standard library math.

Here's an example code snippet that demonstrates how you can use math.Abs() function in Go:

main.go
package main

import (
    "fmt"
    "math"
)

func main() {
    num := -20.5
    absolute := math.Abs(num)
    fmt.Printf("The absolute value of %v is %v\n", num, absolute)
}
176 chars
13 lines

Output:

main.go
The absolute value of -20.5 is 20.5
36 chars
2 lines

In the above example, we have declared a variable num and initialized it with a negative number. Then we have used math.Abs() function to get the absolute value of that number and assigned the result to another variable absolute. Finally, we have printed the original number and its absolute value using fmt.Printf() function.

related categories

gistlibby LogSnag