Why is this allowed

I am a newbie trying to learn Go. In the following piece of code:

package main

import "fmt"

func main() {
	v := 13.25 + 5i // change me!
	fmt.Printf("v is of type %T\n", v)
}

Where the comment is change me is an i. The code runs with i being there but nothing else why is i allowed?

5i means “5 times the square root of minus 1”, this concept is known as complex numbers.

In your example 13.25 is the real part and 5i the imaginary.

PS: This is more a Math-thing than a go thing. The only go specific here is, that go has complex numbers built in, while many other languages do support them via libraries only.

2 Likes

Thank you so much

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.