Simulating a Do-While Loop in Golang
Grace Collins
Solutions Engineer · Leapcell

Key Takeaways
- Go does not have a built-in
do...while
loop, but it can be mimicked using afor
loop. - A
for
loop with an initialtrue
condition ensures execution at least once. - An infinite
for
loop withbreak
can also simulatedo...while
behavior.
In Go, there isn't a built-in do...while
loop as found in some other programming languages. However, you can achieve similar functionality using a for
loop. A common approach is to use a for
loop with a condition that ensures the loop executes at least once. Here's how you can implement it:
package main import "fmt" func main() { var input int for ok := true; ok; ok = (input != 2) { fmt.Println("Press 1 to run") fmt.Println("Press 2 to exit") _, err := fmt.Scanln(&input) if err != nil { fmt.Println("Invalid input") break } switch input { case 1: fmt.Println("Running...") case 2: fmt.Println("Exiting...") // The loop will exit naturally on the next iteration check default: fmt.Println("Invalid selection") } } }
In this example:
- We declare a variable
input
to store user input. - The
for
loop starts withok
set totrue
, ensuring the loop runs at least once. - After each iteration,
ok
is updated based on the condition(input != 2)
. If the user enters2
,ok
becomesfalse
, and the loop terminates. - Inside the loop, we prompt the user and read their input using
fmt.Scanln
. - A
switch
statement handles different cases based on the user's input.
This pattern effectively mimics a do...while
loop, ensuring the code inside the loop executes at least once before the condition is evaluated.
Alternatively, you can use an infinite for
loop with a break
statement to control the loop's termination:
package main import "fmt" func main() { var input int for { fmt.Println("Press 1 to run") fmt.Println("Press 2 to exit") _, err := fmt.Scanln(&input) if err != nil { fmt.Println("Invalid input") continue } switch input { case 1: fmt.Println("Running...") case 2: fmt.Println("Exiting...") break // Exit the loop default: fmt.Println("Invalid selection") } } }
In this version:
- We use an infinite
for
loop:for { ... }
. - The
break
statement is used to exit the loop when the user enters2
. - If the input is invalid, the
continue
statement skips to the next iteration.
Both approaches are valid in Go and can be used to simulate a do...while
loop, depending on your specific requirements and coding style.
FAQs
Go favors simplicity and consistency, using for
as the only looping construct.
Use for ok := true; ok; ok = condition
or an infinite for
loop with a break
statement.
It depends—use for ok := true
for clarity or an infinite loop with break
for flexibility.
We are Leapcell, your top choice for hosting Go projects.
Leapcell is the Next-Gen Serverless Platform for Web Hosting, Async Tasks, and Redis:
Multi-Language Support
- Develop with Node.js, Python, Go, or Rust.
Deploy unlimited projects for free
- pay only for usage — no requests, no charges.
Unbeatable Cost Efficiency
- Pay-as-you-go with no idle charges.
- Example: $25 supports 6.94M requests at a 60ms average response time.
Streamlined Developer Experience
- Intuitive UI for effortless setup.
- Fully automated CI/CD pipelines and GitOps integration.
- Real-time metrics and logging for actionable insights.
Effortless Scalability and High Performance
- Auto-scaling to handle high concurrency with ease.
- Zero operational overhead — just focus on building.
Explore more in the Documentation!
Follow us on X: @LeapcellHQ