Skip to main content
Go Language Fundamentals for Beginners to Advanced
CHAPTER 05 Beginner

Operators in Go

Updated: May 17, 2026
5 min read

# CHAPTER 5

Operators in Go

1. Introduction

Programming is largely about manipulating data. Whether calculating the total price in a shopping cart, checking if a user's age is over 18, or combining multiple security conditions, Operators are the symbols that perform these actions. Go provides a standard, robust set of operators similar to C, Java, and Python.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Perform math using Arithmetic Operators.
  • Compare variables using Comparison Operators.
  • Combine conditions using Logical Operators.
  • Update variables efficiently using Assignment Operators.
  • Understand the basics of Bitwise Operators.

3. Arithmetic Operators

Used for standard mathematical calculations.
OperatorNameExampleResult
+Addition10 + 515
-Subtraction10 - 55
*Multiplication10 * 550
/Division10 / 52
%Modulus (Remainder)10 % 31
++Incrementx++Increases x by 1
--Decrementx--Decreases x by 1

Important Note on Division: If you divide two integers, Go truncates the decimal. 5 / 2 results in 2. To get 2.5, at least one number must be a float64: 5.0 / 2.0.

4. Assignment Operators

Used to assign values to variables.
OperatorExampleEquivalent to
=x = 5Assigns 5 to x
+=x += 3x = x + 3
-=x -= 3x = x - 3
*=x *= 3x = x * 3
/=x /= 3x = x / 3

5. Comparison Operators

Used to compare two values. They always return a bool (true or false).
OperatorMeaningExampleResult
==Equal to5 == 3false
!=Not equal5 != 3true
>Greater than5 > 3true
<Less than5 < 3false
>=Greater or equal5 >= 5true
<=Less or equal5 <= 3false

6. Logical Operators

Used to combine multiple comparison conditions.
OperatorNameDescriptionExample
&&Logical ANDTrue ONLY if BOTH are true(5 > 3) && (2 < 4) is true
||Logical ORTrue if AT LEAST ONE is true(5 < 3) || (2 < 4) is true
!Logical NOTReverses the result!(5 == 5) is false
go
1234567891011
package main
import "fmt"

func main() {
    age := 25
    hasLicense := true

    // Using Logical AND
    canRentCar := (age >= 25) && hasLicense
    fmt.Println("Can rent a car:", canRentCar) // true
}

7. Bitwise Operators (Advanced)

These manipulate data at the raw binary level (1s and 0s). They are mostly used in cryptography, compression, or systems programming.
  • & (AND)
  • | (OR)
  • ^ (XOR)
  • << (Left Shift)
  • >> (Right Shift)

*Note: As a beginner backend developer, you rarely use bitwise operators. Focus on Arithmetic, Comparison, and Logical operators first!*

8. Strict Typing with Operators (Common Mistake)

In Javascript or PHP, you can add a string and a number together ("5" + 2), and the language will silently guess what you mean. Go does not allow this. Go's strict static typing means you cannot perform arithmetic operations on mismatched types.
go
123456789
func main() {
    var a int = 10
    var b float64 = 5.5

    // fmt.Println(a + b) // ERROR: invalid operation: a + b (mismatched types int and float64)
    
    // You MUST convert 'a' to a float64 explicitly
    fmt.Println(float64(a) + b) // Valid: 15.5
}

9. Best Practices

  • Parentheses for clarity: While a + b * c works because of the order of operations, a + (b * c) is instantly readable to other developers.
  • No Pre-increment: In C/C++, you can write ++x or x++. Go *only* allows postfix increments (x++), and it is a statement, not an expression. You cannot write y = x++. This design choice eliminates thousands of confusing bugs found in C.

10. Exercises

  1. 1. Create a variable score := 100. Use the += operator to add 50 to it, then print it.
  1. 2. Create two variables: x := 10 and y := 3. Print the result of x % y and explain what the output means.

11. MCQs with Answers & Explanations

Question 1

What is the result of 10 % 4 in Go?

Question 2

Which operator checks if two values are exactly equal?

Question 3

What does x += 5 mean?

Question 4

If a = true and b = false, what is a && b?

Question 5

What is the Logical OR operator?

Question 6

What does !true return?

Question 7

How does Go evaluate int(5) / int(2)?

Question 8

What happens if you try to add an int and a float64 in Go?

Q9. Is ++x (pre-increment) allowed in Go? a) Yes b) No Answer: b) No. *Explanation: Go only allows postfix x++ to keep the language syntax simple and prevent confusing expression bugs.*

Q10. Can you use x++ inside an assignment like y = x++ in Go? a) Yes b) No Answer: b) No. *Explanation: x++ is a statement in Go, not an expression that returns a value.*

12. Interview Preparation

Interview Questions:
  1. 1. Why does Go prevent operations between an int and a float64?
  1. 2. Explain the difference between == and =.

Common Pitfall: Forgetting that integer division drops the decimal. If you are calculating an average, always convert the integers to float64 before dividing, otherwise your average will be inaccurate.

13. Summary

Operators are the foundation of logic and math in programming. Go utilizes the industry-standard operators found in C and Java but enforces strict type-safety, preventing you from accidentally mixing incompatible data types.

14. Next Chapter Recommendation

Currently, all our data is hardcoded into the program. Real applications require dynamic data from the user. In Chapter 6: User Input and Output, we will learn how to read data typed by the user from the console and format our print statements cleanly.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·