Lesson 4Performing Operations

The previous lesson covered variables, along with the basic data types. In this lesson, you'll learn how to manipulate values and variables using operations. We'll also review some of the operations that you can use with each data type.

The Go programming language supports a variety of operations similar to other programming languages. These include arithmetic operations, relational operations, assignments, and more.

ARITHMETIC OPERATIONS

Go supports most arithmetic operations on all numeric types, including binary and unary operations:

  • A binary operator requires two operands.
  • A unary operator requires one operand.

Arithmetic operations allow you to do common math calculations such as adding, subtracting, multiplying, and dividing. Table 4.1 presents common arithmetic operators available in Go.

Table 4.1 Common arithmetic operators

Operator Description
+ Adds two operands
- Subtracts second operand from the first
* Multiplies both operands
/ Divides the numerator by the denominator
% Modulus operator; gives the remainder after an integer division
++ Increment operator; increases the integer value by 1
-- Decrement operator; decreases the integer value by 1

When writing coding statements for arithmetic ...

Get Job Ready Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.