Multiplication Operator in Go

Golang Multiplication Operator takes two operands and returns the product of these two operands. Both the operands provided to Multiplication Operator should be of same datatype.

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

Multiplication Operator is also called Product Operator.

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

Syntax

The syntax to multiply two operands a and b, using Multiplication Operator is

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.

ADVERTISEMENT

Examples

Multiply Integers

In the following example, we take two integer values and find their product using Multiplication Operator.

Example.go

package main

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

Output

Multiplication (a*b) =  21

Multiply Floating Point Values

In the following example, we take two floating point numbers and find their product using Multiplication Operator.

Example.go

package main

func main() {
	a := 4.1
	b := 2.2
	result := a * b
	println("Multiplication (a*b) = ", result)
}

Output

Multiplication (a*b) =  +9.020000e+000

Multiply Complex Numbers

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

Example.go

package main

func main() {
	a := complex(2, 3)
	b := complex(5, 1)
	result := a * b
	println("Multiplication (a*b) = ", result)
}

Output

Multiplication (a*b) =  (+7.000000e+000+1.700000e+001i)

Multiply Float and Integer

In the following example, we take float value in first operand and integer value in second operand. We try to compute their product using Multiplication Operator.

Example.go

package main

func main() {
	a := 3.14
	b := 2
	result := a * b
	println("Multiplication (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 Multiplication Operator in Go language, with the syntax and example programs.