HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to GoVariablesData TypesOperatorsStringsArray & SliceMap (Dictionary)Conditions (if / switch)Loops (for)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array & Slice
🗂️ Map
🔁 Condition & Loop
🧩 Function
Go›Operators

Operators

Math (+ - * / %), comparison (== < >), and logic (&& || !). Use math.Pow for exponents.

Simple enough for a 5-year-old to understand

Math

Example code
1
package main
2
import "fmt"
3
func main() {
4
fmt.Println(7 % 2)
Remainder: 1
CodeRemainder: 1
5
fmt.Println(7 / 2)
Integer division: 3
CodeInteger division: 3
6
}
Code
💡 Hint: Go does not have **; for exponents, import "math" + math.Pow(2, 10).

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Print the remainder of 23 divided by 4.

🎯 Expected output: 3

Loading editor…

About this topic

Go operators come from C, but the list is intentionally short. For example, there is NO ternary conditional operator — Go designers left it out because it harms readability, and you are expected to write a normal if instead.

Increment and decrement operators exist, but they are STATEMENTS, NOT EXPRESSIONS: they must be on a line by themselves and cannot be embedded inside another expression. This restriction eliminates a lot of confusing behavior found in other languages.

Logical operators short-circuit, which is frequently used in Go: you check if a pointer is nil and then access its field, and if it is nil, the second part is never evaluated.

What you will learn

  • Arithmetic and assignment operators
  • Comparison operators
  • Logical operators and short-circuiting
  • That increment is a statement
  • The absence of the ternary operator and its alternative
  • Bitwise operators

When to use?

The remainder operator is used in odd-even checks and periodic tasks. Short-circuiting, when combined with nil checks, is the most common protection idiom you will see in Go.

Since there is no ternary operator, those who want to make a choice in a single line write small helper functions; the Go tradition is to write a normal if block, which is generally better for readability.

Common mistakes

Wrong

i := 5
j := i++

In Go, increment is a STATEMENT, not an expression; it does not return a value and cannot be placed on the right side of an assignment. You will get a "syntax error: unexpected ++ at end of statement" error. Also, the prefix form does not exist at all.

Correct

i := 5
j := i
i++

Wrong

max := a > b ? a : b

There is NO ternary conditional operator in Go; you will get an "invalid character U+003F '?'" error. This is a deliberate design choice — you are expected to write a normal if.

Correct

max := b
if a > b {
	max = a
}

Wrong

a := 7
b := 2
fmt.Println(a / b)

The division of two integers is also an integer; the result comes out as 3, not 3.5, and the decimal part is truncated. There is no error message, the calculation is silently wrong. To get a decimal result, you must explicitly convert to float64.

Correct

a := 7
b := 2
fmt.Println(float64(a) / float64(b))

Frequently asked questions

Why is there no ternary operator in Go?

Design choice: nested ternary operators become unreadable, and Go puts readability above everything else. The alternative is a normal if block; it is longer, but much easier to understand for someone reading it a month later.

How do I compare strings?

With direct comparison operators — string comparison in Go checks the content and is safe. For case-insensitive comparison, strings.EqualFold is used; it is more correct than converting both to lowercase and comparing.

Why is increment not an expression?

Because both incrementing and reading a variable on the same line is a classic source of confusion that yields different results across languages. Go completely eliminated this ambiguity by making it a statement.

Summary

The Go operator list is intentionally short: there is no ternary conditional operator and increment is a statement, not an expression. Integer division truncates decimals, and string comparison is done safely and directly with operators.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code