Skip to content

Instantly share code, notes, and snippets.

@mikemadisonweb
Created December 11, 2017 12:01
Show Gist options
  • Save mikemadisonweb/972d5c287cde6788ca622e1d0b37ee5a to your computer and use it in GitHub Desktop.
Save mikemadisonweb/972d5c287cde6788ca622e1d0b37ee5a to your computer and use it in GitHub Desktop.
Do while loop in Golang
// Source https://stackoverflow.com/questions/32834661/how-to-replicate-do-while-in-go/32844744#32844744
var input int
for ok := true; ok; ok = (input != 2) {
n, err := fmt.Scanln(&input)
if n < 1 || err != nil {
fmt.Println("invalid input")
break
}
switch input {
case 1:
fmt.Println("hi")
case 2:
// Do nothing (we want to exit the loop)
// In a real program this could be cleanup
default:
fmt.Println("def")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment