Division Operator in Go

Golang Division Operator takes two operands and returns the division of first operand by second operand. Both the operands provided to Division Operator should be of same datatype.

Golang Division Operator applies to integers, floats, and complex values.

Division Operator is also called Quotient Operator.

In this tutorial, we will learn about the syntax of Division Operator, and some example scenarios on how to use Division operator.

Syntax

The syntax to divide a number a by b, using Division Operator is

</>
Copy
a/b

where a and b are operands and / is the operator symbol.

Return Value

The return value is of same datatype as that of first operand.

Examples

Division of Integers

In the following example, we take two integer values and divide the first number a by second number b using Division Operator.

Example.go

</>
Copy
package main

func main() {
	a := 7
	b := 3
	result := a / b
	println("Division (a/b) = ", result)
}

Output

Division (a/b) =  2

When using Division Operator with integers, only quotient is returned, remainder is ignored.

Division of Floating Point Values

In the following example, we take two floating point numbers and find their division of one by another using Division Operator.

Example.go

</>
Copy
package main

func main() {
	a := 7.5
	b := 3.2
	result := a / b
	println("Division (a/b) = ", result)
}

Output

Division (a/b) =  +2.343750e+000

Division of Complex Numbers

In the following example, we take two complex numbers and compute the division.

Example.go

</>
Copy
package main

func main() {
	a := complex(3, 4)
	b := complex(1, 2)
	result := a / b
	println("Division (a/b) = ", result)
}

Output

Division (a/b) =  (+2.200000e+000-4.000000e-001i)

Division of Float by Integer

In the following example, we take float value in first operand and integer value in second operand. We try to compute their division using Division Operator. Since Division Operator accepts operands of same type, the expression of (float / integer) raises an error.

Example.go

</>
Copy
package main

func main() {
	a := 3.14
	b := 2
	result := a / b
	println("Division (a/b) = ", result)
}

Output

./example.go:6:14: invalid operation: a / b (mismatched types float64 and int)

Conclusion

In this Golang Tutorial, we learned about Division Operator in Go language, with the syntax and example programs.