- 01Getting Started
- 02Your First Output (fmt.Println)
- 03Working with Variables
- 04Conditionals (if statements)
- 05Loops (for statements)
- 06Writing a Function
- 07Slices (Arrays)
- 08Loops (a for statement in place of while)
- 09Structs (an Object-Oriented Style)
- 10Error Handling
- 11Sorting a Slice
- 12Recursive Functions
- 13Using the Standard Library
- 14The switch Statement
- 15Anonymous Functions (Closures)
- 16Reusing Code with Struct Embedding
- 17Writing Comments
- 18Logical Operators (&&, ||, !)
- 19Constants (const)
- 20Splitting and Joining Strings (Split, Join)
- 21Writing nil-Safe Code (Checking a Pointer for nil)
- 22Searching a Slice
- 23Transforming a Slice (a map-like Operation)
- 24Two-Dimensional Slices (Tabular Data)
- 25Writing a Custom Error Type
- 26Variadic Parameters (...)
- 27Representing a Set with a Map (Duplicate-Free Data)
- 28Testing with Your Own assert Function
- 29Higher-Order Functions (Passing a Function as an Argument)
- 30Stacks and Queues (Basic Data Structures)
- 31Type Conversion (Casting) Basics
- 32Intro to Regular Expressions (Pattern Matching)
- 33The Binary Search Algorithm
- 34Building a Caesar Cipher (a Character-Shifting Cipher)
- 35Understanding How Bubble Sort Works
- 36Building and Displaying Dates (Basic Year/Month/Day Operations)
- 37Writing Multiple Test Cases Together
- 38Speeding Up Calculations with Memoization (Caching)
- 39Normalizing Strings (TrimSpace, Unifying Case)
- 40Shallow Copy vs. Deep Copy
- 41Enum Basics (iota)
- 42Flattening a Slice
- 43Reversing a String and Checking for Palindromes
- 44Pairing Up Two Slices (the zip operation)
- 45Rounding Numbers (Floor, Ceil, Round)
- 46Multi-Line Strings (Backquotes)
- 47Functions That Return Multiple Values
- 48Finding the GCD and LCM (the Euclidean Algorithm)
- 49Formatting Numbers (Padding Digits, Decimal Places)
- 50Cleanup Logic with defer
- 51Writing Generic Functions
- 52File Reading and Writing Basics
- 53Generating Random Numbers
- 54Bitwise Operators (AND, OR, XOR, shifts)
- 55Reading Command-Line Arguments
- 56Waiting for a Fixed Amount of Time (time.Sleep)
- 57Watch Out for Floating-Point Rounding Errors
- 58Reading from Standard Input
- 59FizzBuzz (the Classic Practice Problem)
- 60Checking Whether a Number Is Prime
- 61Set Operations with Maps (Union, Intersection, Difference)
- 62Converting Number Bases (Binary, Hex)
- 63Checking Balanced Parentheses (an Application of Stacks)
- 64Checking Whether Two Words Are Anagrams
- 65Checking Whether a Year Is a Leap Year
- 66Converting Temperature (Celsius to Fahrenheit)
- 67Prime Factorization
- 68[Applied] Build a Simple Inventory Management Tool
Loops (a for statement in place of while)
In this lesson you will learn to use a condition-only for statement in place of a while loop in Go, and understand conditional repetition. This is for anyone searching for "Go while loop alternative".
Go has no while statement; a for statement with only a condition takes its place. Use the full for statement with initialiser, condition, and update when you already know the stopping point, and the condition-only form when you do not.
The sample code initialises i := 1, then uses the condition-only for i <= 5, increasing the value by one with i++ inside the loop. Exiting the moment the condition becomes false behaves exactly like a while loop in other languages.
A common stumbling block for beginners is forgetting to update the variable the condition depends on. Miss that update and you get an infinite loop. When you actually want one, you can drop the condition entirely and write for {}, leaving it with break.
In real-world development, the condition-only for statement is common where the number of repetitions is not known in advance - work that repeats until a condition is met.
๐งช This site can't compile or run Go directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ nothing is sent anywhere).
