Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Basics of Operators & Expressions | Getting Started
course content

Зміст курсу

Introduction to GoLang

Basics of Operators & ExpressionsBasics of Operators & Expressions

In Go programming, operators are symbols or combinations of symbols that perform various operations on values or variables.

An expression is a combination of values and operators that yield an evaluated value. For example, 7 + 9 is an expression that yields 16, and 7 * 9 is an expression that yields 63, as the * operator represents multiplication.

In this chapter, we will explore the Arithmetic operators. Most of the remaining operators will be discussed in subsequent sections, as relevant.

OperatorFunction
+Addition
-Subtraction
*Multiplication
/Division
%Remainder(Mod)
++Increment
--Decrement

Studying the following code and its corresponding outputs can be a valuable exercise in code comprehension. All the arithmetic operators are elucidated within the code using comments, along with the respective output.

go

index.go

By default, in Go, expressions are evaluated using the BODMAS (also known as PEMDAS) rule. According to this rule, an expression is evaluated in the following order:

  1. Brackets;
  2. Exponents;
  3. Division;
  4. Multiplication;
  5. Addition;
  6. Subtraction;

Consider the expression 1 + 3 * 4 / 2. The order of evaluation and the result are as follows:

1 + 3 * 4 / 21 + 3 * 21 + 67

Hence, fmt.Println(1 + 3 * 4 / 2) outputs 7.

We can use brackets to change the order of operations and, consequently, the result:

(1 + 3) * 4 / 24 * 4 / 24 * 28

Brackets can also be nested for more precise control of operations:

((1 - 3) + 4) / 2(-2 + 4) / 22 / 21

In the above case, subtraction was performed first, followed by addition, and then division.

Все було зрозуміло?

Секція 1. Розділ 7
course content

Зміст курсу

Introduction to GoLang

Basics of Operators & ExpressionsBasics of Operators & Expressions

In Go programming, operators are symbols or combinations of symbols that perform various operations on values or variables.

An expression is a combination of values and operators that yield an evaluated value. For example, 7 + 9 is an expression that yields 16, and 7 * 9 is an expression that yields 63, as the * operator represents multiplication.

In this chapter, we will explore the Arithmetic operators. Most of the remaining operators will be discussed in subsequent sections, as relevant.

OperatorFunction
+Addition
-Subtraction
*Multiplication
/Division
%Remainder(Mod)
++Increment
--Decrement

Studying the following code and its corresponding outputs can be a valuable exercise in code comprehension. All the arithmetic operators are elucidated within the code using comments, along with the respective output.

go

index.go

By default, in Go, expressions are evaluated using the BODMAS (also known as PEMDAS) rule. According to this rule, an expression is evaluated in the following order:

  1. Brackets;
  2. Exponents;
  3. Division;
  4. Multiplication;
  5. Addition;
  6. Subtraction;

Consider the expression 1 + 3 * 4 / 2. The order of evaluation and the result are as follows:

1 + 3 * 4 / 21 + 3 * 21 + 67

Hence, fmt.Println(1 + 3 * 4 / 2) outputs 7.

We can use brackets to change the order of operations and, consequently, the result:

(1 + 3) * 4 / 24 * 4 / 24 * 28

Brackets can also be nested for more precise control of operations:

((1 - 3) + 4) / 2(-2 + 4) / 22 / 21

In the above case, subtraction was performed first, followed by addition, and then division.

Все було зрозуміло?

Секція 1. Розділ 7
some-alt