Go Abs()
Published Jun 9, 2023Updated Jun 11, 2025
Contribute to Docs
The Abs() function returns the absolute value of a given number.
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)isNaN
Example
The following calculates the absolute value of -255 and prints out the result:
package mainimport ("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.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve 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