What is type complex64 in Golang?

Variables contain data, and data can be of different data types, or types for short. Golang is a statically typed language; this means the compiler must know the types of all the variables, either because they were explicitly indicated or because the compiler can infer the type from the code context. A type defines the set of values and the set of operations that can take place on those values.

Type complex64 in Golang is the set of all complex numbers with float32 real and float32 imaginary parts. Below is how you declare a variable of type complex64:

var var_name complex64 = complex(a,b)

Example

package main
import "fmt"
func main() {
var var1 complex64 = complex(3,-5)
var var2 complex64 = complex(2, 7.5)
fmt.Println("var1:", var1)
fmt.Printf("Type of var1: %T", var1)
fmt.Println("\nvar2:", var2)
fmt.Printf("Type of var2: %T", var2)
}

Explanation

The above code declares the variables var1 and var2 with type complex64, and displays the variables and their type using the Println and Printf method respectively.

Copyright ©2024 Educative, Inc. All rights reserved