Skip to main content
In this lesson, you’ll learn how arithmetic operators work in Golang. These operators are essential for building advanced conditional logic when combined with comparison operators. They help you perform common mathematical operations such as addition, subtraction, multiplication, division, modulus (remainder), as well as increment and decrement operations.
The image displays six arithmetic operators: addition, subtraction, multiplication, division, modulus, increment, and decrement, each with their respective symbols.
Below, you will find detailed explanations and examples for each arithmetic operator.

Addition Operator

The addition operator (+) is used to add the left and right operands. Although most commonly applied to numeric values, it also supports string concatenation. Consider the following example:
When executed, the program produces:
Using the addition operator for strings concatenates them, making it a versatile tool in Golang.

Subtraction Operator

The subtraction operator (-) subtracts the right operand from the left operand. This operator works with numeric types but cannot be applied to strings. Attempting string subtraction results in a compilation error:
The error message is as follows:
Below is a correct usage example with numeric values:
Executing this snippet returns:

Multiplication Operator

The multiplication operator (*) multiplies two operands. Here’s an example demonstrating the multiplication of two integers:
When run, the output is:

Division Operator

Using the division operator (/) in Golang returns the quotient of the left operand divided by the right operand. For instance:
This code outputs:
Ensure that when using the division operator with integers, Golang performs integer division, which may truncate any fractional part.

Modulus Operator

The modulus operator (%) returns the remainder from dividing the left operand by the right operand. Check out the example below:
The result of this operation is:

Increment Operator

Golang’s increment operator (++) is a unary operator that increases the value of its operand by one. It must always be used as a post-fix operator. Below is a sample code:
Running this snippet produces:

Decrement Operator

Similarly, the decrement operator (—) is a unary operator that decreases the value of its operand by one and is also used as a post-fix. For example:
When executed, the output is:

That’s all for this lesson. Practice using these arithmetic operators in your Golang projects to enhance your programming skills and understanding of the language. For further reading, consider exploring more about Golang operators in the Golang documentation.

Watch Video

Practice Lab