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›Conditions (if / switch)

Conditions (if / switch)

Parenthesis-free if-else, else if, and multi-branch switch.

Simple enough for a 5-year-old to understand

if / else

In Go, conditions are written WITHOUT parentheses, but { } are mandatory.

Example code
1
package main
2
import "fmt"
3
func main() {
4
score := 75
5
if score >= 50 {
No parentheses, { } are present.
ConditionNo parentheses, { } are present.
6
fmt.Println("Passed")
7
} else {
8
fmt.Println("Failed")
9
}
10
}
Condition
💡 Hint: Go uses == for equality (there is no === since types are strict).

About this topic

Conditions in Go are kept simple. The condition expression is NOT enclosed in parentheses — if you do, gofmt will remove it. Curly braces are MANDATORY, even for single-line blocks. Together, these two rules completely eliminate a class of errors seen in other languages.

A unique and very useful feature of Go is that you can declare a variable right before the condition. The declared variable only lives within those if and else blocks. Error handling is written with this pattern and is the most familiar sight in Go code.

The switch construct in Go is more powerful than in other languages: you don't need to write break because there is NO fall-through behavior, you can write a conditionless switch to use instead of an if-else chain, and you can list multiple values in each branch.

What you will learn

  • the if and else structure, with condition without parentheses
  • defining a variable before the condition
  • the error handling pattern
  • the switch structure and the absence of fall-through
  • building an if-else chain with a conditionless switch
  • the fallthrough keyword

When to use?

Pre-condition declaration is used in every call that returns an error and keeps the scope narrow. You will see this pattern hundreds of times in Go code; it is the concrete embodiment of the language's error-handling philosophy.

A conditionless switch is a more readable equivalent of if-else chains with more than three branches. Each branch can contain a range or a complex condition.

Common mistakes

Wrong

x := 5
if x > 3
	fmt.Println("big")

In Go, curly braces are MANDATORY; omitting them throws the "syntax error: unexpected newline, expected { after if clause" error. This strictness completely eliminates the classic class of errors arising from parenthesis-free blocks.

Correct

x := 5
if x > 3 {
	fmt.Println("big")
}

Wrong

if v, err := doWork(); err != nil {
	fmt.Println(err)
}
fmt.Println(v)

The variable defined in the condition only lives in the if and else blocks; outside, you get an "undefined: v" error. If you are going to use its value afterwards, you must define it before the condition.

Correct

v, err := doWork()
if err != nil {
	fmt.Println(err)
	return
}
fmt.Println(v)

Wrong

switch x {
case 1:
	fmt.Println("one")
	break
case 2:
	fmt.Println("two")
	break
}

Switch branches do not fall through in Go, so break is UNNECESSARY and is just noise. If you intentionally want to fall through, you have to write fallthrough — meaning Go's behavior is the exact opposite of other languages.

Correct

switch x {
case 1:
	fmt.Println("one")
case 2:
	fmt.Println("two")
}

Frequently asked questions

Why is it useful to define a variable before the condition?

It limits the scope of the variable strictly to where it is needed. Error variables do not pollute the function body, and two error variables with the same name do not clash. It is the foundation of Go's error handling pattern.

What is a conditionless switch?

You write the switch without providing a value to compare and place a condition on each branch. It is a more readable equivalent of long if-else chains; range checks and complex conditions look much cleaner this way.

Can I use a number in a condition in Go?

No. The condition must strictly be a boolean; if you write a number, you get a "non-boolean condition in if statement" error. This strictness also makes errors like accidental assignment impossible right from the start.

Summary

In Go, conditions are written without parentheses, curly braces are mandatory, and the condition must be a boolean. Pre-condition variable declaration is the standard pattern for error handling, and in switch there is no fall-through behavior, break is unnecessary.

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