
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: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:Multiplication Operator
The multiplication operator (*) multiplies two operands. Here’s an example demonstrating the multiplication of two integers:Division Operator
Using the division operator (/) in Golang returns the quotient of the left operand divided by the right operand. For instance: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: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: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: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.