What is the real function in Golang?

The real function in Golang is a built-in function that extracts the real part of a given complex number.

The process is illustrated below:

The prototype of the real function is shown below:

func real(c ComplexType) FloatType

Parameters

The real function takes a single mandatory parameter in the form of a ComplexType object.

Return value

The real function returns a FloatType object that represents the real part of the complex number provided as a parameter.

If the given complex number is a complex64 object, then the real function returns a float32 object; if the given complex number is a complex128 object, then the real function returns a float64 object.

Example

The code below shows how the real function works in Golang:

package main
import (
"fmt"
)
func main() {
// initializing complex values
a := complex(15, 2)
b := complex (-5, 4)
// extracting real parts
fmt.Println("The real part of", a , "is", real(a))
fmt.Println("The real part of", b , "is", real(b))
}

Explanation

The complex function in lines 1010 and 1111 initializes a complex object based on the values provided for the real and imaginary parts.

The real function in lines 1414 and 1515 proceeds to extract the real part of each complex object, i.e., 1515 and 5-5, respectively. The extracted real values are then output.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved