What is the Inf function in golang?

The Go programming language uses the Inf function to generate an infinite value.

To use this function, you must import the math package in your file and access the Inf function within it using the . notation (math.Inf). Here, Inf is the actual function, while math is the Go package that stores the definition of this function.

Function definition

The definition of the Inf function inside the math package is:

Parameters

Inf function takes a single argument of int type.

Return value

The Inf function returns a single infinite value of type float64. The sign of this infinite value will be the same as the sign of the argument.

A special case is when the argument is zero, in which case the Inf function treats it as a positive number, i.e., it returns positive infinity.

Examples

Here is a simple example that generates infinite values of different signs:

package main
import(
"fmt"
"math"
)
func main() {
x := -233
y := math.Inf(x)
fmt.Print("The Inf function with the argument ",x, " returns: ", y,"\n")
x = 233
y = math.Inf(x)
fmt.Print("The Inf function with the argument ",x, " returns: ", y)
}

The Inf function always treats 0 as a positive number and so returns positive infinity if the argument is 0:

package main
import(
"fmt"
"math"
)
func main() {
x := 0
y := math.Inf(x)
fmt.Print("The Inf function with the argument ",x, " returns: ", y,"\n")
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved