Fasten your seatbelts, coder! Today, we're immersing ourselves in Go, mastering arithmetic and logical operations on primitive types. These abilities are vital for data manipulation and decision-making during our coding adventure.
Remember that Go's primitive data types include int for whole numbers, float64 for decimal numbers, bool for true/false values, and string for textual content. Both int and float64 exhibit constraints in their numerical spans, which we'll explore when we discuss overflow later in this lesson.
We can perform arithmetic operations — addition (+), subtraction (-), multiplication (*), division (/), and modulus — the remainder of the division (%) — on numerical types. Here's how we do it:
Go supports alteration of order using parentheses and provides the modulus (%) operation, handy for determining whether numbers are even or odd!
Logical operators — && (AND), || (OR), ! (NOT) — function as decision-makers in Go, returning bool values — true or false. Here's how we can utilise them with two bool variables:
In this case, && yields true only if both boolean inputs are true, || outputs true if either of the inputs is true, and ! reverses the boolean value.
However, the primary use of logical operations is with variables. Let's quickly illustrate the basic usage:
