Simulating a Do-While Loop in Golang
Emily Parker
Product Engineer · Leapcell

Key Takeaways
- Go does not have a built-in
do...whileloop, but it can be mimicked using aforloop. - A
forloop with an initialtruecondition ensures execution at least once. - An infinite
forloop withbreakcan also simulatedo...whilebehavior.
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
inputto store user input. - The
forloop starts withokset totrue, ensuring the loop runs at least once. - After each iteration,
okis updated based on the condition(input != 2). If the user enters2,okbecomesfalse, and the loop terminates. - Inside the loop, we prompt the user and read their input using
fmt.Scanln. - A
switchstatement 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
forloop:for { ... }. - The
breakstatement is used to exit the loop when the user enters2. - If the input is invalid, the
continuestatement 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



