Go Abs()

Sriparno08's avatar
Published Jun 9, 2023Updated Jun 11, 2025
Contribute to Docs

The Abs() function returns the absolute value of a given number.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn how to use Go (Golang), an open-source programming language supported by Google!
    • Beginner Friendly.
      6 hours

Syntax

import "math"

result := math.Abs(number)

Where result is the absolute value of number.

  • The result of Abs(-Inf) is +Inf
  • The result of Abs(+Inf) is +Inf
  • The result of Abs(NaN) is NaN

Example

The following calculates the absolute value of -255 and prints out the result:

package main
import (
"fmt"
"math"
)
func main() {
number := math.Abs(-255)
fmt.Printf("%.1f\n", number)
}

The output will be:

255.0

Codebyte Example

The following example is runnable and shows the Abs() function handling an infinite value.

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Go on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn how to use Go (Golang), an open-source programming language supported by Google!
    • Beginner Friendly.
      6 hours