Created
December 11, 2017 12:01
-
-
Save mikemadisonweb/972d5c287cde6788ca622e1d0b37ee5a to your computer and use it in GitHub Desktop.
Do while loop in Golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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