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
if / elseswitchfor (three forms)for ... range
🧩 Function
Go›REFERENCE›Condition & Loop
🔁

Condition & Loop

Decision making and the only loop: for.

CommandDescription
if / elseExecutes code based on a condition.
switchMulti-branch comparison.
for (three forms)Go's only loop.
for ... rangeIterates over a slice/map/string.

if / else

Try it yourself →

Executes code based on a condition.

Syntax

if condition { } else { }

Description

Condition WITHOUT PARENTHESES, blocks with curly braces. Intermediate conditions are added with else if.

Example

package main
import "fmt"
func main() {
    score := 75
    if score >= 50 {
        fmt.Println("Passed")
    } else {
        fmt.Println("Failed")
    }
}

Expected output

Passed

When to use?

Flow branching.

Common mistakes

  • ⚠️ Do NOT put the condition in parentheses; { } are mandatory.

switch

Try it yourself →

Multi-branch comparison.

Syntax

switch value { case x: ... }

Description

In Go, every case breaks automatically (no break needed).

Example

package main
import "fmt"
func main() {
    day := 2
    switch day {
    case 1:
        fmt.Println("Mon")
    case 2:
        fmt.Println("Tue")
    default:
        fmt.Println("?")
    }
}

Expected output

Tue

When to use?

Menu, state control.

for (three forms)

Try it yourself →

Go's only loop.

Syntax

for i := 0; i < n; i++ { }

Description

for handles all kinds of loops: classic (3 parts), conditional (like while: for condition {}), and infinite (for {}).

Example

package main
import "fmt"
func main() {
    for i := 1; i <= 3; i++ {
        fmt.Println(i)
    }
}

Expected output

1
2
3

When to use?

Known repetition.

for ... range

Try it yourself →

Iterates over a slice/map/string.

Syntax

for i, e := range slice { }

Description

range provides the index (or key in a map) along with each element. Use _ if the index is not needed.

Example

package main
import "fmt"
func main() {
    for _, color := range []string{"red", "purple"} {
        fmt.Println(color)
    }
}

Expected output

red
purple

When to use?

Iterating over lists/maps.

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