OSGo.003: Operators and Expressions — Arithmetic, Comparison, Logic, Assignment, and Bitwise Operations

Unique cyberpunk mechanical computation factory representing Go arithmetic, comparison, logical, assignment, and bitwise operators.

Elementary Overview

Operators tell Go what computation to perform on values. Arithmetic operators calculate, comparison operators produce Boolean results, logical operators combine conditions, assignment operators update variables, and bitwise operators work directly with integer bits. Expressions combine values, operators, function calls, and other language elements into something Go can evaluate.

This lesson follows OSGo.002: Variables, Constants, Zero Values, and Basic Types. OSGo.002 introduced the values operators act on; OSGo.003 focuses on how those values are combined and transformed.

John Elder introduces arithmetic, assignment, increment/decrement, and comparison operators in Go.

Learning Objectives

  • Use Go arithmetic operators: +, -, *, /, and %.
  • Explain integer division.
  • Use ==, !=, <, <=, >, and >= for comparisons.
  • Combine Boolean conditions with &&, ||, and !.
  • Use = and compound assignments such as += and *=.
  • Understand why ++ and -- are statements rather than expressions.
  • Use bitwise &, |, ^, &^, <<, and >> with integer values.
  • Recognize how precedence affects larger expressions.
  • Explain why Go intentionally has no ?: ternary operator.

Arithmetic Operators

Go uses familiar arithmetic operators. Addition uses +, subtraction uses -, multiplication uses *, division uses /, and remainder uses %. The operand types determine the type and behavior of the result.

Example:
a := 17
b := 5
fmt.Println(a + b)   // 22
fmt.Println(a - b)   // 12
fmt.Println(a * b)   // 85
fmt.Println(a / b)   // 3
fmt.Println(a % b)   // 2

Because both operands in a / b are integers, Go performs integer division and discards the fractional part. To produce a floating-point result, use floating-point operands or perform an explicit conversion when appropriate.

Floating-point example:
x := 17.0
y := 5.0
fmt.Println(x / y)   // 3.4

Comparison Operators

Comparison expressions answer questions and produce a Boolean result. Go provides == for equality, != for inequality, < and > for ordering, and <= and >= for inclusive comparisons.

Example:
temperature := 82
fmt.Println(temperature == 82)   // true
fmt.Println(temperature != 82)   // false
fmt.Println(temperature > 75)   // true
fmt.Println(temperature <= 90)   // true

Philomathia demonstrates comparison and logical operators in Go with Boolean expressions.

Logical Operators

The logical operators work with Boolean values. The operator && means logical AND, || means logical OR, and ! means logical NOT. Go evaluates && and || from left to right and only evaluates the right side when the result still depends on it. This behavior is called short-circuit evaluation and is defined by the current Go language specification.

Example:
online := true
temperature := 78
safe := online && temperature < 85
alert := !online || temperature >= 85
fmt.Println(safe)
fmt.Println(alert)

BRO-IT demonstrates comparison, AND, OR, and NOT logic in Go.

Assignment and Compound Assignment

The assignment operator = stores a value in an already-declared variable. Compound assignment combines an operation with assignment. For example, x += 3 is shorthand for x = x + 3.

Example:
power := 100
power += 25
power -= 10
power *= 2
power /= 5
fmt.Println(power)

Do not confuse = with :=. The short declaration operator := declares at least one new variable in the current scope, while = assigns to variables that already exist. That distinction builds directly on OSGo.001: What Is Go? and the variable rules introduced in OSGo.002.

Increment and Decrement

Go supports postfix increment and decrement statements: x++ and x--. Unlike C, C++, Java, or JavaScript, these are statements rather than expressions. You cannot place x++ inside a larger expression or use a prefix form such as ++x.

Example:
count := 10
count++
count--
fmt.Println(count)

The Go FAQ explains that removing ++ and -- from the expression hierarchy avoids complicated order-of-evaluation cases and keeps expression syntax simpler.

Bitwise Operators

Bitwise operations work on integer representations. Go provides & for bitwise AND, | for OR, ^ for XOR, &^ for AND NOT or bit clear, << for left shift, and >> for right shift. These operations become especially useful in networking, embedded systems, masks, registers, binary protocols, and low-level data processing.

Example:
a := uint8(12)   // binary 00001100
b := uint8(10)   // binary 00001010
fmt.Println(a & b)
fmt.Println(a | b)
fmt.Println(a ^ b)
fmt.Println(a << 1)
fmt.Println(a >> 1)

ProgrammingKnowledge’s Go course includes integer types, bitwise operators, shifts, and later control-flow topics.

Operator Precedence

When an expression contains several operators, precedence determines which operation binds first. Multiplication, division, remainder, shifts, and several bitwise operators bind more tightly than addition and subtraction. Comparison operators bind below arithmetic, while && binds more tightly than ||. Parentheses should be used whenever they make intent clearer.

Example:
result := 2 + 3*4
fmt.Println(result)   // 14

explicit := (2 + 3) * 4
fmt.Println(explicit)   // 20

Go Has No Ternary Operator

Go intentionally does not provide the C-style conditional operator condition ? trueValue : falseValue. The official Go FAQ says the language designers preferred the clearer if/else form because ternary expressions can become difficult to read when they grow or nest.

Go form:
status := “offline”
if online {
    status = “online”
}
fmt.Println(status)

A Go community discussion—written humorously—shows why the absence of a ternary operator remains a recurring language-design topic.

Expressions and Type Compatibility

Go is strongly typed, so operands generally must be compatible with the operator being used. A Boolean cannot be added to an integer, and a string cannot be multiplied by a number. Explicit conversion is required when a calculation legitimately crosses numeric types.

Example:
count := 5
ratio := 2.5
total := float64(count) * ratio
fmt.Println(total)

This same principle appears across statically typed languages. Compare it with OSC#.003: C# Operators and Expressions to see which operator ideas transfer between languages and which rules are specific to Go.

Mini Lab

  1. Create a project with go mod init operatorslab.
  2. Create two integer variables and print their sum, difference, product, quotient, and remainder.
  3. Compare the two values with ==, !=, <, and >.
  4. Create two Boolean conditions and combine them with &&, ||, and !.
  5. Use += and *= to update an integer variable.
  6. Use ++ once and -- once as standalone statements.
  7. Choose two uint8 values and test &, |, ^, <<, and >>.
  8. Create one expression where parentheses intentionally change the result.
  9. Write a normal if/else block where another language might use a ternary operator.
  10. Run gofmt, go vet, and go run .

Knowledge Check

  1. What does % calculate?
  2. Why does 17 / 5 produce 3 when both operands are integers?
  3. Which operator tests equality?
  4. What is the difference between && and ||?
  5. What is the difference between = and :=?
  6. Can x++ be embedded inside a larger Go expression?
  7. What does &^ do?
  8. Why are parentheses useful even when you know precedence rules?
  9. Does Go provide a ?: ternary operator?

Answers

  1. The remainder after integer division.
  2. Integer division produces an integer result and discards the fractional portion.
  3. ==.
  4. && requires both Boolean operands to be true; || requires at least one to be true.
  5. = assigns to existing variables; := is the short variable declaration form and must introduce at least one new variable in the current scope.
  6. No. ++ and -- are statements, not expressions.
  7. It performs bit clear, also described as AND NOT.
  8. They make evaluation order and programmer intent explicit.
  9. No. Go intentionally uses if/else instead.

Primary References

Elementary Conclusion

Go’s operator system is intentionally compact. Arithmetic transforms numeric values, comparisons produce Boolean answers, logical operators combine decisions, assignment changes program state, and bitwise operations expose integer bits directly. Go also removes some expression complexity found in related languages: ++ and -- are statements, and there is no ternary operator. Learn these rules now and later control-flow, networking, concurrency, and systems code will read much more naturally.

BitcoinVersus.Tech Editor’s Note:
We volunteer daily to ensure the credibility of the information on this platform is Verifiably True. If you would like to support to help further secure the integrity of our research initiatives, please donate using the QR code provided

Bitcoin donation QR code for 5,000 sats.

For best QR-code scanning results, please scan this donation code from a desktop or larger screen. Mobile screens may prevent camera recognition.

Leave a Reply