Golang real function
last modified May 8, 2025
This tutorial explains how to use the real
built-in function in Go.
We'll cover complex number basics with practical examples of extracting real parts.
The real function is used to extract the real component from complex numbers in Go. It works with both complex64 and complex128 types. The function returns a floating-point value representing the real part.
In Go, real
is essential for complex number operations. It's often
used with imag
to separate complex numbers into components. The
function is built into the language and requires no imports.
Basic real function example
The simplest use of real
extracts the real part from a complex
number. This example demonstrates basic real component extraction.
Note: Complex numbers are written as (real + imaginary)i.
package main import "fmt" func main() { c := complex(3.5, 2.1) r := real(c) fmt.Printf("Complex: %v\n", c) fmt.Printf("Real part: %.2f\n", r) // Alternative complex number syntax c2 := 4.2 + 7.9i fmt.Printf("\nComplex2: %v\n", c2) fmt.Printf("Real part: %.2f\n", real(c2)) }
The real
function extracts 3.5 from the first complex number.
The second example shows the alternative complex number literal syntax.
Using real with complex64
The real
function works with both complex64 and complex128 types.
This example demonstrates using real with the smaller complex64 type.
package main import "fmt" func main() { var c complex64 = complex(1.2, 3.4) r := real(c) fmt.Printf("Type: %T\n", c) fmt.Printf("Value: %v\n", c) fmt.Printf("Real part: %v (type: %T)\n", r, r) // Operations with the real part scaled := r * 2 fmt.Printf("Scaled real part: %.2f\n", scaled) }
The output shows the real part is extracted as float32 for complex64. We demonstrate type safety by showing the extracted component's type.
Real part in mathematical operations
The real component can be used in mathematical calculations. This example shows practical use of the real part in computations.
package main import ( "fmt" "math" ) func main() { c := complex(5.0, 12.0) // Represents 5 + 12i r := real(c) i := imag(c) // Calculate magnitude using real and imaginary parts magnitude := math.Sqrt(r*r + i*i) fmt.Printf("Complex number: %v\n", c) fmt.Printf("Magnitude: %.2f\n", magnitude) // Use real part in comparison if r > 0 { fmt.Println("Real part is positive") } }
We calculate the magnitude of a complex number using its real and imaginary parts. The real part is also used in a conditional check.
Real function with arrays
The real
function can process arrays of complex numbers. This
example demonstrates extracting real parts from multiple complex values.
package main import "fmt" func main() { numbers := []complex128{ complex(1, 2), complex(3, 4), complex(5, 6), } // Extract all real parts reals := make([]float64, len(numbers)) for i, num := range numbers { reals[i] = real(num) } fmt.Println("Complex numbers:", numbers) fmt.Println("Real parts:", reals) // Calculate average of real parts sum := 0.0 for _, r := range reals { sum += r } fmt.Printf("Average real: %.2f\n", sum/float64(len(reals))) }
We create a slice of real parts from complex numbers. The example also shows calculating statistics on the extracted real components.
Real part in function return values
Functions can return complex numbers, and we can extract their real parts. This example demonstrates using real with function return values.
package main import ( "fmt" "math/cmplx" ) func rotate(c complex128, angle float64) complex128 { // Rotate complex number by angle (in radians) return c * cmplx.Rect(1, angle) } func main() { point := complex(1, 0) // Point on real axis // Rotate by 90 degrees (π/2 radians) rotated := rotate(point, 3.14159/2) fmt.Printf("Original: %.2f\n", point) fmt.Printf("Rotated: %.2f\n", rotated) fmt.Printf("New real part: %.2f\n", real(rotated)) // Check if real part is approximately zero if math.Abs(real(rotated)) < 1e-9 { fmt.Println("Rotation successful (real part ≈ 0)") } }
The example rotates a complex number and examines its real part. We use the
real
function to verify the rotation's effect.
Source
This tutorial covered the real
function in Go with practical
examples of complex number processing and real component extraction.
Author
List all Golang tutorials.